What is method chaining in jQuery?

With jQuery, we can chain multiple methods on an element. We do not need to select the same element multiple times to implement a method on it. Rather, we can just add the methods together into one single line of code.

Syntax

$(element).action1().action2()...

Code

In the example below, when you click the button, the paragraph element, which has the id test, has methods called on it one after the other using method chaining.

First, we change the text color and then slide the paragraph element up and down.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources