In the docs example end() is not doing anything useful, it's just
showing where end() fits. I use it to avoid repeating selectors:

$('#myform')
   .find('input')
      .click(fn..).end()
   .find('textarea')
      .mouseover(fn...).end()
   .find('label')
      .css('color', 'red');

Limiting the search to #myform descendants gives me faster results,
and end() allows for a nice chaining structure.

- ricardo

On Apr 22, 5:15 pm, JKippes <jessandthec...@gmail.com> wrote:
> MorningZ and Ignacio, Thanks so much for the examples.  Both helped me
> understand .end() better.
>
> I have a couple additional questions to solidfy my understanding.
>
> Is this example,
>     $("div").find("span").css('background':'red').end().css
> ('background':'green­');
> mainly where you see .end() being used?  For when you want to attach
> different property settings to elements within the same container?
>
> In the example provided on the page I was viewing,
>     $("a").filter(".clickme").click(function(){ alert("You are now
> leaving the site."); }).end()
> can you describe what .end() is doing?  There isn't anything in the
> chain after that point, and I'm not sure what it would be backing up
> to.
>
> Thanks again.
>
> On Apr 22, 1:28 pm, Ignacio Cortorreal <luis3igna...@gmail.com> wrote:
>
> > if you do something like:
>
> > $("div").find("span").css('background':'red').end().css('background':'green­');
>
> > spans will be red, and  the divs will be green.
>
> > .end() reset the chain to the first selector
>
> > On Wed, Apr 22, 2009 at 2:05 PM, MorningZ <morni...@gmail.com> wrote:
>
> > > Say you have the html of
>
> > > <div>
> > >    <span>One</span>
> > >    <span>Two</span>
> > >    <span>Three</span>
> > > </div>
>
> > > and say:
> > > var $obj = $("div");
>
> > > your jQuery object, $obj, will be just the <div> tag
>
> > > Now if you say
>
> > > var $obj = $("div").find("span");
>
> > > that would first be an object representing the <div> and the ".find()"
> > > makes it be an object of the 3 <span> tags
>
> > > If the statement was (and granted this doesn't make sense, but just an
> > > example)
>
> > > var $obj = $("div").find("span").end();
>
> > > that would be just the <div> tag again....  although walking through
> > > the selector, $obj would have been the <div>, then would have
> > > represented the "found" <span> tags, called ".end()" backs off the
> > > ".find()" and goes back to the div
>
> > > thinking of the chained command like a deck of cards helps :-)
>
> > > On Apr 22, 12:41 pm, JKippes <jessandthec...@gmail.com> wrote:
> > > > Please referencehttp://docs.jquery.com/How_jQuery_Works, the
> > > > Chainability segment.
>
> > > > I'm confused on the given description of .end():
>
> > > > You can take this even further, by adding or removing elements from
> > > > the selection, modifying those elements and then reverting to the old
> > > > selection, for example:
> > > > $("a")
> > > >    .filter(".clickme")
> > > >      .click(function(){
> > > >        alert("You are now leaving the site.");
> > > >      })
> > > >    .end()
>
> > > > <a href="http://google.com/"; class="clickme">I give a message when you
> > > > leave</a>
>
> > > > Methods that modify the jQuery selection and can be undone with end(),
> > > > are the following:
>
> > > > add(),
> > > > children(),
> > > > eq(),
> > > > filter(),
> > > > find(),
> > > > next(),
> > > > not(),
> > > > parent(),
> > > > parents(),
> > > > siblings() and
> > > > slice().
>
> > > > What does reverting and undone mean here?  When I run the code, the
> > > > link click event runs the alert, I hit Ok, and it passes me to
> > > > Google.  So the words reverting and undone confuse me, as I would take
> > > > this to mean the modified click event would be "undone" and never
> > > > executed.   So what does .end() really do?  A friend thinks it could
> > > > be a chain terminator, though he never uses it.  Is it just a cleanup
> > > > thing part of good practice and not technically needed?
>
> > > > Thanks for educating a real jQuery beginner.- Hide quoted text -
>
> > - Show quoted text -

Reply via email to