jQuery — Callback
Basic Callback
$("p").hide(400, function() {
console.log("Animation complete!");
});
Callback with fadeIn
$("p").fadeIn(600, function() {
$(this).addClass("visible");
});
Callback in Chaining
$("div")
.slideUp(300)
.slideDown(300, function() {
console.log("Slide complete");
});
Multiple Effects, One Callback
$("p").hide(200, function() {
$(this).text("Hidden!").show(200);
});
Callback vs Promise
// Callback style
$("p").fadeOut(400, function() {
console.log("Done");
});
// Promise style (jQuery 1.5+)
$("p").fadeOut(400).done(function() {
console.log("Done");
});
// jQuery Deferred
var def = $("p").fadeOut(400);
def.done(function() { console.log("Success"); });
def.fail(function() { console.log("Failed"); });
def.always(function() { console.log("Always"); });
practical Example
$("#deleteBtn").click(function() {
$("#item").fadeOut(300, function() {
$(this).remove();
$("#status").text("Item deleted");
});
});
Mini Practice
- Add a callback to hide()
- Chain effects with a callback at the end
- Use .done() for promise-style
- Remove an element after an animation
Up Next
Continue with Chaining — linking multiple jQuery methods together.
Related Topics
Frequently Asked Questions about Callback
What is Callback in jQuery?
Callback is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Callback?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Callback.
Why is Callback important in jQuery?
Callback is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.