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,438
  • 68
  • 55
  • 126
pedrum golriz
  • 515
  • 8
  • 26

3 Answers3

3

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

$('tr > td:nth-child(3):contains("$")').remove();
dsgriffin
  • 64,660
  • 17
  • 133
  • 135
  • 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
  • 35,267
  • 10
  • 97
  • 106
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
  • 22,910
  • 4
  • 23
  • 48