2

I would like to remove div first, then show my popup. But while showing the alert I am able to see the div. Once closing the popup the div will be removed.

$("#btnRemoveDiv").on("click",function(){$("#divRemove").remove();
alert("Removed");});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div id="divRemove">Remove this div first then show the popup. Dont show this while the popup is opened!</div>
<input type="button" id="btnRemoveDiv" value="Remove">
enter image description here

@quirimmo Please see this:

Community
  • 1
  • 1
divya
  • 196
  • 11

2 Answers2

8

Use when to manage it as a promise and it should work:

$("#btnRemoveDiv").on("click",function(){
  $.when($('#divRemove').remove()).then(function() {alert('removed');});
});
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<div id="divRemove">Remove this div first then show the popup. Dont show this while the popup is opened!</div>
<input type="button" id="btnRemoveDiv" value="Remove">

JSfiddle:

https://jsfiddle.net/91x9ta3n/12/

quirimmo
  • 9,800
  • 3
  • 30
  • 45
1

Use a timeout so the alert and the remove doesn't execute at the same time.

$("#btnRemoveDiv").on("click", function () {
    $("#divRemove").remove();
    setTimeout(function () {
        alert("Removed");
    }, 500);
});

jsfiddle: https://jsfiddle.net/91x9ta3n/7/

Majid Parvin
  • 4,499
  • 5
  • 29
  • 47