If it is just one element you can do:

  var myelem = $('#myelem')[0];

Because the jQuery "wrapper" acts like an array you can just grab the
first element of the array, which is the "raw" DOM element.

Another thing to keep in mind is jQuery's "chainability". So while
it's not so "efficient" to deal with just one object and do one thing
to it, if you want to do multiple things to one object (or many
objects) it becomes very "efficient".

For example:

  $('#myelem')
    .click( function(){ alert('blah'); } )    // assign a click event handler
    .css( backgroundColor, "#000" );   // change the background color

It's not quite like using "with", but it's pretty close.

Karl rudd

On 3/9/07, Rob Wilkerson <[EMAIL PROTECTED]> wrote:
> I knew about each(), but since I had only one element it seemed...I
> don't know...almost like overkill.  I was hoping there would be
> something like:
>
> $('myele').do (
>      /** do stuff */
> );
>
> But, that having been said, I guess each() is effectively that.  Maybe
> it's only the semantics of it that had me looking for a different way.
>
> Thanks for your help.
>
> On 3/8/07, Karl Rudd <[EMAIL PROTECTED]> wrote:
> > Something to also keep in mind is that most of the jQuery functions
> > have an implicit "each" included with them. A jQuery "object" is
> > basically an array of objects, so the methods you run on the object
> > are actually run on all the objects in the array.
> >
> > For instance, given the follow HTML:
> >
> > <ul>
> >   <li>1</li>
> >   <li>2</li>
> >   <li>3</li>
> > </ul>
> >
> > The following script will change the background color of all the "li" 
> > elements:
> >
> >   $('li').css({backgroundColor:"#000"});
> >
> > It's like writing:
> >
> > var elems = document.getElementsByTagName("li");
> > for ( var i = 0; i < elems.length; i++ )
> >   elems[i].style.backgroundColor = "#000";
> >
> > This is where jQuery gets a lot of it's "write less, do more" power from.
> >
> > Perhaps you should take a look at some of the tutorials:
> >
> >   http://docs.jquery.com/Tutorials
> >
> > Karl Rudd
> >
> > On 3/9/07, Jake McGraw <[EMAIL PROTECTED]> wrote:
> > > Not exactly, to add to Chris's comment, using:
> > >
> > > $("myele").each(function() {
> > >   // do lots of stuff
> > > });
> > >
> > > will scope the 'this' keyword to whatever you've selected using 
> > > $('myele').
> > >
> > > So, for example, if I had:
> > >
> > > with(document.getElementById("myele")) {
> > >   // myele now part of scope chain, no variable needed
> > >   style.backgroundColor = "#000";
> > > }
> > >
> > > I could replace it using the following jQuery:
> > >
> > > $("#myele").each(function(){
> > >   // myele now referenced using "this"
> > >   this.style.backgroundColor = "#000";
> > > });
> > >
> > > Keep in mind that $(selector).each() will work with all elements that have
> > > been found using a given selector (see http://docs.jquery.com/Core).
> > >
> > > Also, this is just my opinion, but using the "with" keyword is usually a 
> > > bad
> > > idea, as it is difficult to optimize such code and it can cause surprising
> > > behavior when defining functions within such blocks. Instead, just assign
> > > the element to a variable, using jQuery or JavaScript, like:
> > >
> > > var myele = $("#myele");
> > > myele.css({backgroundColor:"#000"});
> > >
> > > is equivalent to
> > >
> > > var myele = document.getElementById("myele");
> > > myele.style.backgroundColor = "#000";
> > >
> > > - jake
> > >
> > >
> > > On 3/8/07, Chris Domigan <[EMAIL PROTECTED]> wrote:
> > > >
> > > > You can use .each().
> > > >
> > > > $("#myId").each(function() {
> > > >   // do lots of stuff
> > > > });
> > > >
> > > > _______________________________________________
> > > > jQuery mailing list
> > > > discuss@jquery.com
> > > > http://jquery.com/discuss/
> > > >
> > > >
> > >
> > >
> > > _______________________________________________
> > > jQuery mailing list
> > > discuss@jquery.com
> > > http://jquery.com/discuss/
> > >
> > >
> >
> > _______________________________________________
> > jQuery mailing list
> > discuss@jquery.com
> > http://jquery.com/discuss/
> >
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to