0

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.

moondog
  • 1,537
  • 5
  • 22
  • 34

3 Answers3

1

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>?

daniel0mullins
  • 1,937
  • 5
  • 21
  • 45
  • OMG+LOL. "Any particular reason you can't/don't want to use – moondog Feb 14 '12 at 02:12
  • It's all good! A book I **highly** suggest is _Javascript: The Good Parts_ by Douglas Crockford. I think you would find it useful! (That's where I picked up the – daniel0mullins Feb 14 '12 at 02:15
  • Thanks for the tip. I will definitely pick this up. – moondog Feb 14 '12 at 02:39
0
$.when($('#qwe').remove()).then(alert('qwe removed!'));
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Alex
  • 11
0

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

Yup it means exactly that I'm afraid.

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

Community
  • 1
  • 1
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • Oh, that's too bad. Do you know if there some trick or work-around that would allow me execute a chunk of jquery code only after the div gets removed? – moondog Feb 14 '12 at 01:27
  • Yes. Any statement immediately following `$('div.myclass').remove();` will execute when the remove action is complete. This is just normal, synchronous program flow. – Beetroot-Beetroot Feb 14 '12 at 01:39
  • 1
    @Beetroot: It seems that sometimes the code following $('div.myclass').remove(); is executed before remove() is complete. I will update my question and explain my problem in more detail. – moondog Feb 14 '12 at 01:50