Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 1. jQuery Basics > Including the Previous Selection with the Current Se... - Pg. 20

.end().end().length); //alerts 3 </script> </body> </html> The first alert() statement in the code contains a jQuery statement that will search the document for all <p> elements and then apply filter() to the selected <p> elements in the set selecting only the one(s) with a class of middle . The length property then reports how many elements are left in the set: alert(jQuery('p').filter('.middle').length); //alerts 1 The next alert() statement makes use of the end() method. Here we are doing every- thing we did in the prior statement except that we are undoing the filter() method and returning to the set of elements contained in the wrapper set before the filter() method was applied: alert(jQuery('p').filter('.middle').end().length); //alerts 3 The last alert() statement demonstrates how the end() method is used twice to remove both the filter() and find() destructive changes, returning the wrapper set to its orig- inal composition: alert(jQuery('p').filter('.middle').find('span').end().end().length); //alerts 3 Discussion If the end() method is used and there were no prior destructive operations performed,