agyanadda@gmail.com

jQuery Chaining and Callbacks

jQuery Effects 1 min read Updated Dec 1, 2026 Topic 10 of 25

Chaining

Most jQuery methods return the same jQuery object, so you can call another method right after:

$('#msg')
  .text('Saved!')
  .css('color', 'green')
  .slideDown()
  .delay(1500)
  .slideUp();

The element is selected only once, which is shorter and faster.

Put each method on a new line for readability.

Callbacks

Effects run asynchronously — the next line of code does not wait. Code that must run after an effect goes in a callback:

$('#popup').fadeOut(400, function () {
  $(this).remove(); // runs only when fading is done
});

Getters break the chain

Methods that return a value (.text(), .val(), .attr('href') without a new value) cannot be chained further.

Example

Example — Chaining and callback HTML
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<button id="go">Send message</button>
<p id="status" style="display:none;padding:8px"></p>
<div id="card" style="margin-top:10px;padding:14px;background:#fff3cd;width:220px">Click the card to remove it</div>

<script>
  $(function () {
    $('#go').click(function () {
      $('#status')
        .stop(true, true)
        .text('Message sent ✓')
        .css({ background: '#d3f9d8', color: '#2b8a3e' })
        .slideDown(300)
        .delay(1500)
        .slideUp(300);
    });

    $('#card').click(function () {
      $(this).fadeOut(500, function () {
        $(this).remove();
        alert('Card removed after the fade finished.');
      });
    });
  });
</script>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Why can jQuery methods be chained?

Exercise 2 True / False

Code written on the line after fadeOut() waits until the fade finishes.

Exercise 3 Fill in the Blank

A function passed to run after an effect ends is called a ___.

Frequently Asked Questions

It clears the animation queue and jumps the current animation to its end, which stops effects from piling up when a button is clicked many times.