Jquery removes certain children

I have the following code:

$('.TopNotificationIcon span').remove(); 

Is it possible to replace .TopNotificationIcon with this , but inside this specific class only span exists.

This is the structure

 <div class="TopNotificationIcon"><span>xxxxx</span></div> 

When you click .TopNotificationIcon , the span should be removed.

+8
javascript jquery
source share
5 answers

If you have a click event for .TopNotificationIcon , you can do something like this

 $('.TopNotificationIcon').click(function(){ $('span',this).remove(); }); 
+17
source share

I would use the find () method as it seems to be the fastest:

 $("div.TopNotificationIcon").click(function() { $(this).find("span").remove(); }); 
+7
source share

Try it...

 $('span').remove('.TopNotificationIcon'); 

This will remove all span elements with the TopNotificationIcon class, as well as child elements

+2
source share

Yes, but you need to change the line to:

 $(this).children('span').remove(); 

js fiddle: http://jsfiddle.net/UNhhh/1/

+1
source share

If you want to delete the entire range in TopNotification, you can do this:

 $('div').live('click', function(){ $(this).children('span').remove(); }); 

It will remove all children in the div.

0
source share

All Articles