jQuery callback function executes when the effect is complete. In the previous tutorials we have discussed several jQuery effects, each of those methods accept callback function as a parameter that executes when the effect is finished. For example if we are passing a callback function to slideUp() method then the callback function will execute when the slide up effect is complete.
jQuery Callback Function Example
In the following example we are passing a callback function to the hide() method as a parameter, in the callback function we are setting up an alert message. Since the callback function executes when effect is finished, in this case the alert message will display once the paragraph is hidden.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide("slow", function(){ alert("The paragraph is now hidden"); }); }); }); </script> </head> <body> <h2>jQuery callback function example</h2> <p>This example is published on beginnersbook.com. This para will disappear when you click on the hide button and an alert message will display that "The paragraph is now hidden"</p> <button>Hide</button> </body> </html>
Output:
Before the button is clicked:
After the button is clicked:
Leave a Reply