1

Exactly as the title says, I'm trying to remove the entire row of a table if a match occurs. I have the following:

$('tr > td:nth-child(3)').text().match(/\$/g);

So in this case, if the third column contains a '$', I would want the entire row to be removed. I'm not sure how to go about this since the above only returns an array with a length of the number of matches and each index is the matched string.

Pseudo:

if($('tr > td:nth-child(3)').text().match(/\$/g)){
    $(this).remove();
}

I've been programming all day so my brain is fried, I'm sure there's something very simple I'm overlooking. Any help is appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
pedrum golriz
  • 513
  • 8
  • 27

3 Answers3

3

I believe you could use :contains, in this case:

$('tr > td:nth-child(3):contains("$")').remove();
dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • good call, didn't even think about contains. However this removes the column, not the row. It should be `$('tr > td:nth-child(3):contains("$")').parent('tr').remove();` – pedrum golriz Jun 18 '14 at 02:24
3

Just a slight nuance of what null suggested will remove the entire row:

$('tr > td:nth-child(3):contains("$")').parent().remove();

jsFiddle

Alex W
  • 37,233
  • 13
  • 109
  • 109
0

The match method returns an array. You want to use the test method instead.

$('tr').filter(function() {
    return /\$/.test( $(this).find('td:nth-child(3)').text() );
})
.remove();
PeterKA
  • 24,158
  • 5
  • 26
  • 48