jQuery - Chaining
You may link actions/methods together using jQuery.
Chaining allows us to execute many jQuery methods (on the same element) within one line or sentence.
Related Links
jQuery Method Chaining
So far we wrote jQuery statements one by one (one after the other).
There is, however, a method known as chaining that allows us to perform several jQuery instructions on the same element one after the other (s).
Tip : This eliminates the need for browsers to search for the same element(s) several times.
An action is chained when it is appended to the preceding action.
Example :- The example below combines the techniques css()
, slideUp()
and slideDown()
in a single line. The "p1" element will first be changed to red,
then slid up and down.
jQuery is fun!!
Output
jQuery is fun!!
Tip : The code line might be rather lengthy when chaining. JQuery is not extremely stringent with regard to syntax; you may style it how you like, including line breaks and indentations.
Example 2 :- This also works just fine:
jQuery is fun!!
Output :-
jQuery is fun!!
Extra whitespace is removed by jQuery, and the lines above are executed as a single long line of code.
Related Links