jQuery Chaining
Most jQuery methods return the same jQuery object they were called on. That means you can call another method on the result straight away — a technique called method chaining:
$("#title").css("color", "crimson").slideUp(400).slideDown(400);The selector runs once, and three operations are applied in order. Without chaining you would write:
const $title = $("#title");
$title.css("color", "crimson");
$title.slideUp(400);
$title.slideDown(400);Both are fine; chaining is just shorter and avoids repeating the selector.
A chain in action
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
#banner { padding: 12px; background: #0769ad; color: #fff; width: 260px; border-radius: 6px; }
</style>
</head>
<body>
<button id="go">Run chain</button>
<p id="banner">Chaining is neat.</p>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$("#go").on("click", function () {
$("#banner")
.stop(true, true)
.text("Watch me!")
.css("background", "#c0392b")
.slideUp(400)
.slideDown(400)
.fadeTo(300, 0.4)
.fadeTo(300, 1, function () {
$(this).text("Chaining is neat.").css("background", "#0769ad");
console.log("chain finished");
});
});
</script>
</body>
</html>Two things to notice:
- Line breaks and indentation are allowed anywhere between calls — format long chains one method per line.
.text()and.css()run immediately, while the effects are queued. That is why the text changes before the slides start, and the final reset is placed in the last callback.
What can be chained
| Method kind | Returns | Chainable? |
|---|---|---|
Setters: .text("x"), .css("a","b"), .attr("a","b"), .addClass() |
the jQuery object | ✅ |
Effects: .hide(), .fadeIn(), .animate() |
the jQuery object | ✅ |
Events: .on(), .off(), .trigger() |
the jQuery object | ✅ |
Getters: .text(), .val(), .css("color"), .attr("href") |
a value (string, number) | ❌ ends the chain |
.length |
a number | ❌ |
A getter returns a plain value, so you cannot continue with jQuery methods after it.
$("#name").val().addClass("x"); // ❌ TypeError: .addClass is not a function
$("#name").addClass("x").val(); // ✅ returns the value at the endTraversing inside a chain: end()
Traversal methods such as .find(), .children() or .filter() return a new set. .end() goes back to the previous set, letting a single chain work on several related groups:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
.card { border: 1px solid #ccc; border-radius: 8px; padding: 0 12px 8px; width: 260px; }
</style>
</head>
<body>
<div class="card" id="c">
<h3>Plan: Pro</h3>
<ul>
<li>Unlimited projects</li>
<li>Priority support</li>
<li class="new">Team sharing</li>
</ul>
<button>Choose</button>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$("#c")
.css("background", "#f9fbfd")
.find("h3").css("color", "#0769ad").end() // back to #c
.find("li.new").append(" 🆕").css("font-weight", "bold").end()
.find("button").text("Choose Pro").on("click", function () {
console.log("chose:", $(this).closest(".card").find("h3").text());
});
</script>
</body>
</html>| Method | What it does in a chain |
|---|---|
.find(sel) |
Step into descendants matching sel |
.filter(sel) |
Narrow the current set |
.end() |
Return to the set before the last traversal |
.addBack() |
Add the previous set to the current one |
Keeping chains readable
Long chains are powerful but can turn into puzzles. Some guidelines:
- One method per line, indented under the selector.
- Avoid more than one or two
.end()calls; store intermediate sets in variables instead. - Put effects that depend on each other in callbacks, or use
.promise().
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
.tag { display: inline-block; padding: 4px 10px; margin: 3px; border-radius: 12px; background: #ddd; cursor: pointer; }
.tag.on { background: #0769ad; color: #fff; }
</style>
</head>
<body>
<div id="tags">
<span class="tag">news</span><span class="tag">sport</span><span class="tag">music</span><span class="tag">tech</span>
</div>
<p>Selected: <b id="sel">none</b></p>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
const $tags = $("#tags .tag");
$tags.on("click", function () {
$(this)
.toggleClass("on")
.fadeTo(80, 0.5)
.fadeTo(80, 1);
const chosen = $tags.filter(".on").map(function () {
return $(this).text();
}).get();
$("#sel").text(chosen.join(", ") || "none");
console.log("selected:", chosen);
});
</script>
</body>
</html>Test yourself
Why can most jQuery methods be chained?
- They return
undefined - They return the jQuery object they were called on
- They return a Promise
- They modify the global
$
Which call ends a chain because it returns a plain value?
.addClass("a").css("color", "red").attr("href").hide()
In $("#c").find("h3").hide().end().addClass("x"), which element gets class x?
- The
h3 #c- Both
- Neither