aWebDeveloper
aWebDeveloper

Reputation: 38422

jquery remove specific children

I have the following code :

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

Can I replace .TopNotificationIcon with this i.e only span exists inside this specific class.

This is the structure

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

On click of .TopNotificationIcon, span should be removed.

Upvotes: 16

Views: 24416

Answers (5)

Code Maverick
Code Maverick

Reputation: 20415

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

$("div.TopNotificationIcon").click(function() {

    $(this).find("span").remove();    

});

Upvotes: 15

Brice Favre
Brice Favre

Reputation: 1527

If you want to remove all span under TopNotification you can do this :

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

It will remove all children in a div.

Upvotes: 4

Matt
Matt

Reputation: 1038

Yes but youd need to change the line to:

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

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

Upvotes: 3

MNIK
MNIK

Reputation: 1629

Try this...

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

This will remove all span elements with the class TopNotificationIcon and also child elements

Upvotes: 2

Jishnu A P
Jishnu A P

Reputation: 14390

if you have click event for .TopNotificationIcon you can do something like this

$('.TopNotificationIcon').click(function(){
    $('span',this).remove();    
});

Upvotes: 19

Related Questions