Chaining
Chaining jQuery methods allows you to call a series of methods on a selection without temporarily storing the intermediate values. This is possible because every setter method that we call returns the selection on which it was called. This is a very powerful feature and you will see it being used by many professional libraries. Consider the following example:
$( '#button_submit' ) .click(function() { $( this ).addClass( 'submit_clicked' ); }) .find( '#notification' ) .attr( 'title', 'Message Sent' );x
In this snippet, we are chaining click()
, find()
, and attr()
methods on a selector. Here, the click()
method is executed, and once the execution finishes, the find()
method locates the element with the notification
ID and changes its title
attribute to a string.