Method chaining in jQuery
Method chaining is a feature of jQuery that allows several methods to be executed on a jQuery selection in sequence in a single code statement. For example, the following snippets of code are equivalent:
Without chaining:
$( "button#play-movie" ).on( "click", playMovie );
$( "button#play-movie" ).css( "background-color", "orange" );
$( "button#play-movie" ).show();
With chaining:
$( "button#play-movie" ).on( "click", playMovie )
.css( "background-color", "orange" )
.show();
Notice that with chaining, the button only needs
to be selected one time, whereas without chaining,
jQuery must search the whole DOM and find the button
before each method is applied. Thus, in addition to
yielding more concise code, method chaining in jQuery
offers a potentially powerful performance advantage.
Note: To be entirely precise, it should be noted
that method chaining in jQuery is not the only way
to avoid repetitively searching the entire DOM.
