JMax2012
JMax2012

Reputation: 373

Remove LI from UL based on text within a SPAN

Given the following code:

<ul>
<li class="parent">
    <a href="#link">
        <span>Home</span>
    </a>
</li>
<li class="parent">
    <a href="#link">
        <span>Contact</span>
    </a>
</li>
<li class="parent">
    <a href="#link">
        <span>Blog</span>
    </a>
</li>   

How can I check the value of the SPAN text, and if it equals "Blog," remove the entire LI from the UL?

The jQuery code I've been messing with is getting too complex, and I'm sure there's some kind of nested "find" approach that will help me solve this using minimal code.

Upvotes: 0

Views: 991

Answers (3)

Dave
Dave

Reputation: 4436

jsfiddle

$("span").each(function() {
     if( $(this).text()=='Blog') 
      $(this).closest('li.parent').remove(); 
});

Upvotes: 0

Felix
Felix

Reputation: 38112

You can do:

$('ul li span').each(function() {
    if($.trim($(this).text())=="Blog") {
        $(this).closest('li').remove();
    }
});

Fiddle Demo

Upvotes: 0

j08691
j08691

Reputation: 207900

$('span:contains("Blog")').closest('li').remove()

jsFiddle example

Upvotes: 2

Related Questions