@भि कहिन

my personal web log

Archive for the ‘Javascript and jQuery’ Category

Javascript and jQuery

Problem with jQuery fadeOut with remove

Posted by @bhitalks on March 29, 2010

I faced this peculiar problem when trying to remove an element from DOM after fading it out. Ditto for slideUp or other similar methods.

This is what I was trying to do;

$('#elem').fadeOut('slow', function() { $('#elem').remove(); });

But this wasn't working out as expected. It should have removed the element from the DOM after it finished fadeOut on the element. But to my frustation, it wouldn't. The element was very much there albeit hidden owing to its display set to 'none' courtesy fadeOut.

As usual, my first saviour being Google, I googled. Only to find the same snippet (as above) being suggested on several sites!

After much struggling with it, found out that I was attempting to remove the element within the context, which in effect was failing.

The correct way would be to call the method on the context reference, i.e. 'this'. So the technique becomes:

$('#elem').fadeOut('slow', function() { $(this).remove(); });

And this works like a charm!

-@bhitalks

Posted in Javascript and jQuery | Tagged: , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.