Prasanth T
Prasanth T

Reputation: 31

How to find a child element with a specific value and delete it's immediate parent node?

I need to find an element <strong class="name">Priority:</strong> . If that exists I need to delete the parent element of it i.e <div class="wrap"> I know there are other elements also exists in <li class="item">. e.g <strong class="name">Type:</strong>. but that shouldn't touched.

<li class="item">
    <div class="wrap">
        <strong class="name">Type:</strong>
        <span id="type-val" class="value">
             <img alt="Task" height="16" src="task.png" title="" width="16"> Task
        </span>
    </div>
</li>
<li class="item">
        <div class="wrap">
            <strong class="name">Priority:</strong>
            <span id="priority-val" class="value editable-field inactive" title="Click to edit">
                   <img alt="2. Critical" height="16" src="/critical.png" title="2"> 2. Critical
            <span class="overlay-icon aui-icon aui-icon-small aui-iconfont-edit"></span></span>
        </div>
</li>

I tried below code, but it didn't help.

$("li.item").find("strong.name").each(function(){
if (this[value="Priority:"]) {
    this.parent.remove()
}
});

Upvotes: 2

Views: 135

Answers (3)

madalinivascu
madalinivascu

Reputation: 32354

   $('.name').each(function(){
     $val = $(this).text();

     if ($val == "Priority:") {
       $(this).parent().remove();
     };
  });

Upvotes: 0

Shaunak D
Shaunak D

Reputation: 20626

Use filter() and closest('li') to delete parent li.

$("li.item strong.name").filter(function(){
    return $(this).text().indexOf('Priority:') > -1;
}).closest('li').remove();
  //to remove <div class="wrap"> use parent() instead of closest(..)

or use :contains()

$("li.item strong.name:contains(Priority:)").closest('li').remove();

Upvotes: 1

haim770
haim770

Reputation: 49095

You can try to combine :has with :contains. For example:

$('li.item .wrap:has(strong.name:contains("Priority:"))').remove();

See Fiddle

Upvotes: 1

Related Questions