jQuery -

Callback


What is a jQuery Callback?

In jQuery, a callback is a function that is executed after the current effect is finished. Callbacks are essential for creating smooth and complex animations and interactions in your web applications. This tutorial covers various aspects of using callbacks in jQuery with detailed explanations, examples, and useful tips.


1. Basic Callback

You can use a callback function to execute code after an animation completes.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback - Basic</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#slideUpButton").click(function(){
                $("div").slideUp("slow", function(){
                    alert("Slide up animation completed.");
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="slideUpButton">Slide Up</button>
</body>
</html>
        
    

This example demonstrates how to execute a callback function after an element slides up.


2. Callback with Animation

You can use callbacks with different jQuery animation methods like .fadeIn(), .fadeOut(), and .slideToggle().

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback with Animation</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeOutButton").click(function(){
                $("div").fadeOut("slow", function(){
                    alert("Fade out animation completed.");
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeOutButton">Fade Out</button>
</body>
</html>
        
    

This example shows how to use a callback function with the .fadeOut() method.


3. Chaining Animations with Callback

You can chain multiple animations using callbacks to create complex animation sequences.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Chaining Animations with Callback</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#startAnimationButton").click(function(){
                $("div").slideUp("slow", function(){
                    $(this).slideDown("slow", function(){
                        $(this).fadeOut("slow");
                    });
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="startAnimationButton">Start Animation Sequence</button>
</body>
</html>
        
    

This example demonstrates how to chain animations using callback functions.



4. Callback with Event Handling

You can use callback functions in event handlers to execute code after an event is triggered.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback with Event Handling</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#clickButton").click(function(){
                $("div").slideUp("slow", function(){
                    alert("Click event handled and slide up animation completed.");
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="clickButton">Click Me</button>
</body>
</html>
        
    

This example shows how to use a callback function in a click event handler.


5. Callback with Ajax

You can use callbacks with jQuery Ajax methods to execute code after an Ajax request completes.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback with Ajax</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#loadDataButton").click(function(){
                $.ajax({
                    url: "https://jsonplaceholder.typicode.com/posts/1",
                    success: function(result){
                        $("div").html(result.title).fadeIn("slow", function(){
                            alert("Ajax request completed and data loaded.");
                        });
                    }
                });
            });
        });
    </script>
</head>
<body>
    <button id="loadDataButton">Load Data</button>
    <div style="display:none; background-color: yellow; padding: 10px;"></div>
</body>
</html>
        
    

This example demonstrates how to use a callback function with the .ajax() method.


6. Callback with Delayed Execution

You can use callbacks with the .delay() method to delay the execution of code.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback with Delayed Execution</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#delayedButton").click(function(){
                $("div").slideUp("slow").delay(1000).slideDown("slow", function(){
                    alert("Delayed slide down animation completed.");
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="delayedButton">Start Delayed Animation</button>
</body>
</html>
        
    

This example shows how to delay the execution of a callback function.


7. Nested Callbacks

You can use nested callbacks to create complex sequences of animations and actions.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Nested Callbacks</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#startAnimationButton").click(function(){
                $("div").slideUp("slow", function(){
                    $(this).slideDown("slow", function(){
                        $(this).fadeOut("slow", function(){
                            alert("Animation sequence completed.");
                        });
                    });
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="startAnimationButton">Start Animation Sequence</button>
</body>
</html>
        
    

This example demonstrates how to use nested callbacks to create a sequence of animations.



8. Callback with Promises

You can use promises with callbacks to handle complex asynchronous operations more efficiently.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback with Promises</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#startAnimationButton").click(function(){
                $("div").slideUp("slow").promise().done(function(){
                    $(this).slideDown("slow").promise().done(function(){
                        $(this).fadeOut("slow").promise().done(function(){
                            alert("Animation sequence completed.");
                        });
                    });
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="startAnimationButton">Start Animation Sequence</button>
</body>
</html>
        
    

This example shows how to use the .promise() and .done() methods with callbacks.


9. Callback with Animation Queue

You can manage the animation queue using callbacks to control the order of animations.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback with Animation Queue</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#startAnimationButton").click(function(){
                var div = $("div");
                div.animate({left: "250px"}, {queue: "custom"});
                div.animate({top: "100px"}, {queue: "custom"});
                div.dequeue("custom").promise().done(function(){
                    alert("Animation sequence completed.");
                });
            });
        });
    </script>
    <style>
        div {position: relative; background-color: yellow; width: 100px; height: 100px;}
    </style>
</head>
<body>
    <button id="startAnimationButton">Start Animation Sequence</button>
    <div>This is a div element.</div>
</body>
</html>
        
    

This example demonstrates how to manage the animation queue with callbacks.


10. Callback with Custom Functions

You can define custom functions as callbacks to reuse code and create modular animations.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery Callback with Custom Functions</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function slideUpAndFadeOut(element) {
            element.slideUp("slow", function(){
                element.fadeOut("slow", function(){
                    alert("Custom function animation completed.");
                });
            });
        }

        $(document).ready(function(){
            $("#startAnimationButton").click(function(){
                slideUpAndFadeOut($("div"));
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="startAnimationButton">Start Custom Function Animation</button>
</body>
</html>
        
    

This example shows how to use custom functions as callbacks.