jQuery Chaining

With jQuery, you can link actions/methods together.

Chaining allows us to run multiple jQuery methods (on the same element) in one statement.

jQuery method link

Until now, we have written one jQuery statement at a time (one after the other).

However, there is a technique called chaining that allows us to run multiple jQuery commands on the same element, one after the other.

Tip : In this case, the browser does not have to find the same element multiple times.

To link an action, simply add the action to the previous action.

The following example links css(), slideUp(), and slideDown() together. The "p1" element will first turn red, then swipe up, then swipe down:

Run and try it out

If necessary, we can also add multiple method calls.

Tip: When you link, the line of code becomes very poor. However, the jQuery syntax is not very strict; you can write in the desired format, including line breaks and indents.

The following writing can also work well:

Run and try it out

Hint : jQuery will throw away extra spaces and treat it as a line of long code to execute the above line of code.

Last Updated: 2018-11-24 14:46:26 By Akshay


share_facebook share_twitter share_google_plus

Please Comment Here :