tags:

views:

41

answers:

4

I would like to remove the following table from the page with Jquery by targeting the following text "To add the above items to your cart".

I tried this but didn't work as expected it removes a different parent td. Not sure how to target this td only.

$("* :contains('To add the above items to your cart')").closest('td').remove();

<td colspan="11" class="colors_backgroundlight" align="right"> 
<span>To add the above items to your cart, click the "Add" checkboxes then click "Add To Cart" &gt;&gt;&gt;</span> <input type=submit value="Add To Cart" onclick="refineResults=false;" name="checkboxes_mode_submitbtn"

+5  A: 

You can do it like this:

$("span:contains('To add the above items to your cart')").closest('td').remove();

Any parent will also contain the text, since it's in a child element. You need to be a bit more specific about what contains the text and move up from there...otherwise it'll find all the parents as well.

Or, to be very accurate, but probably overkill:

$("span:contains('To add the above items to your cart')").filter(function() {
  return this.innerHTML.indexOf('To add the above items to your cart') === 0;
}).closest('td').remove();
Nick Craver
The first one works as expected but I think I like the second one even better cause it guarantees that it will remove the right td.Both work but in the second you have "Add" instead of "add"
@user357034 - Woops, manually typed that in...corrected the casing :)
Nick Craver
Thanks for the help!!! Sometimes we just get a block and cannot figure out the simply stuff. And yes that td was nested.
@Nick Craver: I think this extension (http://dev.jquery.com/ticket/6561) to the `:contains` selector would work nicely as well.
fudgey
@fudgey - It would, but it's not *quite* the same, since you could have 20 nested tags, without other text inside, `.text()` would be the same for all. Here's an example: http://jsfiddle.net/BsdXc/ This is why my second example uses `.innerHTML` instead of `.text()` ;)
Nick Craver
@Nick Craver: Ahhh, thanks! I've updated my ticket ;)
fudgey
+1  A: 

Remove the table or just the table cell?... removing just the cell will break your table layout. Try this instead (to clear out the table cell)

$("span:contains('To add the above items to your cart')").empty();

Edited above, Nick is right, it'll break nested cells, the update above won't.

fudgey
this one I already tried and didn't work as expected.
+1  A: 

Why not simply use:

$("td:contains('To add the above items to your cart')").remove();​
galambalazs
In the case of nested tables, this would break...a nested `<span>` is much less likely :)
Nick Craver
but he didn't mentioned that the text will be in a span. It might be only an example...
galambalazs
@galambalazs - I'd wager good money that's his actual code...that's a pretty convoluted "example" :)
Nick Craver
An example of what he wants to find. But he may want to remove other texts which may **not** be inside `span`... He only mentioned that he wants to remove `td` s :)
galambalazs
this one I also tried and didn't work as expected.
well you showed almost no code and expect it to work well in all circumstances? You're lucky Nick's solution fits your needs. Go with it, but ask wisely next time.
galambalazs
A: 

Besides the answers that handle the exact problem, you could alter your html by adding a class to that span and target that instead..

so the

<td colspan="11" class="colors_backgroundlight" align="right"> 
<span>To add the above items to your cart, click the "Add" checkboxes then click "Add To Cart"...

could become

<td colspan="11" class="colors_backgroundlight" align="right"> 
<span class="removable">To add the above items to your cart, click the "Add" checkboxes then click "Add To Cart"...

and then targeted with

$(".removable").closest('td').remove();

or for more complex scenarios where you could have different rules for removal of nodes..

$(".removable:contains('To add the above items to your cart')").closest('td').remove();
Gaby