0

I have a table with the following structure:

| abc|1 | def | ghi |
| erg|1 | asd | dfg |
| sdf|2 | ghj | erd |
| tsd|2 | sdf | hgj |

I now want to hide all tr where the suffix |2 can be found in the first td. So the result should be:

| abc|1 | def | ghi |
| erg|1 | asd | dfg |

This is my approach:

$('table tr').each(function() {
  $(this).find("\\|2").hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>bla|1</td>
    <td>sdf</td>
    <td>hfgh</td>
  </tr>
  <tr>
    <td>bla|1</td>
    <td>3sf</td>
    <td>gdfg</td>
  </tr>
  <tr>
    <td>bla|1</td>
    <td>sdf</td>
    <td>jfhj</td>
  </tr>
  <tr>
    <td>bla|2</td>
    <td>sdf</td>
    <td>hfgh</td>
  </tr>
  <tr>
    <td>bla|2</td>
    <td>3sf</td>
    <td>gdfg</td>
  </tr>
  <tr>
    <td>bla|2</td>
    <td>sdf</td>
    <td>jfhj</td>
  </tr>
</table>

jsfiddle.

But that doesn't produce any result.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181

5 Answers5

1

You can use :contains selector.

$('table tr:contains(|2)').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>bla|1</td>
    <td>sdf</td>
    <td>hfgh</td>
  </tr>
  <tr>
    <td>bla|1</td>
    <td>3sf</td>
    <td>gdfg</td>
  </tr>
  <tr>
    <td>bla|1</td>
    <td>sdf</td>
    <td>jfhj</td>
  </tr>
  <tr>
    <td>bla|2</td>
    <td>sdf</td>
    <td>hfgh</td>
  </tr>
  <tr>
    <td>bla|2</td>
    <td>3sf</td>
    <td>gdfg</td>
  </tr>
  <tr>
    <td>bla|2</td>
    <td>sdf</td>
    <td>jfhj</td>
  </tr>
</table>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
0

That's not how .find() works. Try this instead:

$('table tr').each(function() {
  if ($(this).children('td').eq(0).text().match("\\|2")) {
    $(this).hide();
  }
});

Fiddle

J. Titus
  • 9,535
  • 1
  • 32
  • 45
0

you can use .endsWith

$('table tr').each(function() {
    var $this = $(this);
    if ($this.find('td').eq(0).text().endsWith("|2")) {
        $this.hide();
    }
});

what actually happening here is we are finding whether the content of first column of every row endsWith |2

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
0

You have to find td which contains "|2":-

$(this).find('td:contains("|2")').parent('tr').hide();

here is the fiddle

Bik
  • 553
  • 10
  • 31
0

Simply use the following code

$('tr:contains(|2)').hide();

Update:

To restrict to check in first td only,

$('tr td:first-child:contains(|2)').parent('tr').hide();