tags:

views:

46

answers:

3

If I have some html like this.

<ul class="menu">
 <li>
  <a href="pageGreen">
   pageGreen
  </a>
 </li>
 <li>
  <a href="pageBlue">
   pageBlue
  </a>
 </li>
 <li>
  <a href="pageRed">
   pageRed
  </a>
 </li>
 <li>
  <a href="page1">
   page1
  </a>
 </li>
 <li>
  <a href="page2">
   page2
  </a>
 </li>
 <li>
  <a href="page3">
   page3
  </a>
 </li>
</ul>

How would I remove the entire LI element for pageGreen and pageBlue only, using jquery?

+4  A: 

something like this could work

$("a[href='pageGreen']").parent().remove();

easement
Yeah, that should do the trick.
Philip Morton
much better than my attempt (im a jQuery noob) :)
Sergio
+2  A: 

You could try something like....

$("li a").each(function() {
if($this).html() == "pageGreen" || $(this).html() == "pageBlue") {
$(this).closest("li").fadeOut();
} 
});

Might need tweaking

Sergio
That's a lot of unnecessary traversal / comparisons. You'd be going through each li and then each a. It makes more sense to start with the a and then work outward/upward.
easement
yeah, after seeing your suggested answer I totally agree. Much prettier.
Sergio
This works, but there are MUCH better options available.
Sonny Boy
yeah....thats already been stated...twice
Sergio
+1  A: 

Try jQuery’s :contains() selector:

$("li:contains(pageGreen), li:contains(pageBlue)").remove();
Gumbo