Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>With a Callback (the fix)</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style> #box{width:150px;height:80px;background:#04AA6D;color:#fff;display:flex;align-items:center;justify-content:center;margin-bottom:10px;} </style> </head> <body> <button id="btn">Fade Out and Remove</button> <div id="box">Box</div> <div id="status" style="font-weight:bold;"></div> <script> $("#btn").on("click", function () { $("#status").text("Fading out..."); $("#box").fadeOut(1500, function () { $(this).remove(); // runs only after the fade finishes $("#status").text("Removed! (this time, after the fade completed)"); }); }); </script> </body> </html>
Output