user1080247
user1080247

Reputation: 1166

remove all child nodes with specfic class

i have dynamic ul li nodes that make tree and i want to remove all child span nodes which have

class="highlight" or
class="add_action" or
class="delete_action" or
class="edit_action"

under specfic li with specfic id -(in this example 20 )- i tried this code with jquery to find all span with theses classes to remove it but it didnt worked

$('li#20').find('span.add_action').each(function(){
    $(this).remove();
});

also tired

   $('li#20').eq(0).find('.add_action').remove();

  $('li#20').children('.add_action').remove();

this the full example

https://jsfiddle.net/kqagjtmr/

Upvotes: 1

Views: 108

Answers (3)

Cerbrus
Cerbrus

Reputation: 72857

You have duplicate id attributes in your code, for example:

<li class="thide" id="20"><span class="vertical"></span>

<span id="20" class="first_name" title="">الجد  سعد</span>

This is why $('li#20') doesn't work properly. id attributes must be unique, and shouldn't start with a number, either. Use classes instead.

To remove elements, simply use:

$('someSelector').remove();

Also, you should include jQuery in your fiddle, you can find the option here:

enter image description here

Upvotes: 2

Nikhil Batra
Nikhil Batra

Reputation: 3148

See the updated fiddle : "https://jsfiddle.net/kqagjtmr/1/"

Use $('li#20').find("highlight add_action delete_action edit_action").remove();

to find multiple elements at once and remove them.

Upvotes: 1

Tushar
Tushar

Reputation: 87203

Get all the span elements having the specified class and remove all of those. No need of using each to looping. Use the span elements comma-separated to select all the matching elements.

$('li#20')
    .find('span.highlight, span.add_action, span.delete_action, span.edit_action')
    .remove();

DEMO

Upvotes: 2

Related Questions