tags:

views:

599

answers:

1
+3  Q: 

end() function

The end() function in jQuery reverts the element set back to what it was before the last destructive change, so I can see how it's supposed to be used, but I've seen some code examples, eg: on alistapart (which were probably from older versions of jQuery - the article is from 2006) which finished every statement off with .end(). eg:

$( 'form.cmxform' ).hide().end();
  • Does this have any effect?
  • Is it something I should also be doing?
  • What does the above code even return?
+4  A: 

That end() doesn't do anything. There's no point to coding like that. It will return $('#myBox') -- the example is pretty poor. More interesting is something like this:

$('#myBox').show ().children ('.myClass').hide ().end ().blink ();

Which will show myBox, hide the specified children, and then blink the box. There are more interesting examples here:

http://simonwillison.net/2007/Aug/15/jquery/

such as:

$('form#login')
    // hide all the labels inside the form with the 'optional' class
    .find('label.optional').hide().end()
    // add a red border to any password fields in the form
    .find('input:password').css('border', '1px solid red').end()
    // add a submit handler to the form
    .submit(function(){
        return confirm('Are you sure you want to submit?');
    });
John Millikin
oh yeah, i mean, i understand that use of it, i'm just wondering if there was some of reasoning behind code like the example.
nickf
No, the example you posted is utterly silly.
John Millikin
yeah that's what I thought... that's pretty much exactly the code I got from alistapart (http://alistapart.com/articles/prettyaccessibleforms). ALA is usually pretty good with their techniques, so I was wondering if there was something I was missing.
nickf