moondog
moondog

Reputation: 1577

how to execute jQuery callback on $('div.myclass').remove()?

I would like to execute a callback after a particular div gets removed. I tried this, but it does not work:

$('div.myclass').remove(function(){
 alert ("div removed");                                 
});

Anyone know how to execute such a callback? I checked the jQuery documentation for remove(), but it does not mention anything about callbacks. Does this mean remove() does not support a callback?

UPDATE: Here is a more detailed explanation of what I am trying to accomplish. I have a div with class 'nojavascript'. I want to display this div only if the user does not have javascript enabled. Here is the code:

$(document).ready(function() {
  $('div.nojavascript').remove(); 
  ... // additional jquery commands
});

The problem: this div is sometimes displayed to the user for a brief moment, even though javascript is enabled. It's as though the page is displayed to the user before .remove() finishes executing. I am trying to ensure that the user never sees this div, even for a brief moment, if javascript is enabled.

Upvotes: 0

Views: 1328

Answers (3)

Alex
Alex

Reputation: 11

$.when($('#qwe').remove()).then(alert('qwe removed!'));

Upvotes: 0

daniel0mullins
daniel0mullins

Reputation: 1959

It seems like you might be have better luck using <noscript>. This type of thing is exactly what the tag is designed for.

<html>
<head>...</head>
<body>
...
<noscript>
    <div class="noscript">This doesn't display to users who have JavaScript enabled</div>
</noscript>
<div>
    <p> This displays to JavaScript and non-Javascript alike!</p>
</div>
...
</body>
</html>

Any particular reason you can't/don't want to use <noscript>?

Upvotes: 1

elclanrs
elclanrs

Reputation: 94101

Does this mean remove() does not support a callback?

Yup it means exactly that I'm afraid.

And then.... jQuery remove() callback?

Upvotes: 0

Related Questions