Telerik blogs

This article is taken from How jQuery Works.

 

jQuery uses an interesting concept called a "Builder" to make its code short and simple. The Builder pattern is an object-oriented programming design pattern that has been gaining popularity.

In a nutshell: Every method within jQuery returns the query object itself, allowing you to 'chain' upon it, for example:

$("a")
   .filter(".clickme")
     .click(function(){
       alert("You are now leaving the site.");
     })
   .end()
   .filter(".hideme")
     .click(function(){
       $(this).hide();
       return false;
     })
   .end();
 

Which would work against the following HTML:

<a href="http://google.com/" class="clickme">I give a message when you leave</a>
 <a href="http://yahoo.com/" class="hideme">Click me to hide!</a>
 <a href="http://microsoft.com">I'm a normal link</a>

Methods that modify the jQuery selection and can be undone with end(), are the following:

  • add(),
  • children(),
  • eq(),
  • filter(),
  • find(),
  • gt(),
  • lt(),
  • next(),
  • not(),
  • parent(),
  • parents() and
  • siblings().

Please check the Traversing API documentation for details of these methods.


About the Author

Iana Tsolova

is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.

Comments

Comments are disabled in preview mode.