jQuery Callback Functions
A callback function is executed after the current effect is 100% finished.
jQuery Callback Functions
JavaScript statements are executed line by line.
However, with effects,
the next line of code can be run even though the effect is not finished.
This can create errors.
To prevent this, you can create a callback function.
A callback function is executed after the current effect is finished.
Syntax: $(selector).hide(speed,callback);
Example : With Callback
The example below has a callback parameter that is a function that will be executed after the
hide effect is completed:
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden"); }); });
Example : Without Callback
The example below has no callback parameter, and the alert box will be displayed before the
hide effect is completed:
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden"); });
Callback Without Callback
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<script <script
src="https://ajax.googleapis.com/ajax/libs/jquer src="https://ajax.googleapis.com/ajax/libs/jquer
y/3.7.1/jquery.min.js"></script> y/3.7.1/jquery.min.js"></script>
<script> <script>
$(document).ready(function(){ $(document).ready(function(){
$("button").click(function(){ $("button").click(function(){
$("p").hide("slow", function(){ $("p").hide(1000);
alert("The paragraph is now hidden"); alert("The paragraph is now hidden");
}); });
}); });
}); </script>
</script> </head>
</head> <body>
<body> <button>Hide</button>
<button>Hide</button> <p>This is a paragraph with little content.</p>
<p>This is a paragraph with little content.</p> </body>
</body> </html>
</html>
jQuery - Chaining
With jQuery, you can chain together actions/methods.
Chaining allows us to run multiple jQuery methods (on the same element) within a single
statement.
jQuery Method Chaining
Until now we have been writing jQuery statements one at a time (one after the other).
However, there is a technique called chaining,
that allows us to run multiple jQuery commands,
one after the other, on the same element(s).
Tip: This way, browsers do not have to find the same element(s) more than once.
To chain an action, you simply append the action to the previous action.
The following example chains together the css(), slideUp(), and slideDown() methods. The "p1"
element first changes to red, then it slides up, and then it slides down:
Example
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
});
});
</script>
</head>
<body>
<p id="p1">jQuery is fun!!</p>
<button>Click me</button>
</body>
</html>
We could also have added more method calls if needed.
Tip: When chaining, the line of code could become quite long. However, jQuery is not very strict
on the syntax; you can format it like you want, including line breaks and indentations.
This also works just fine:
Example : slideUp - slideDown - extra whitespace
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
});
});
</script>
</head>
<body>
<p id="p1">jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is
fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is
fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!!
jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!! jQuery is fun!!</p>
<button>Click me</button>
</body>
</html>
Note: jQuery throws away extra whitespace and executes the lines above as one long line of code.