Fazil Mir
Fazil Mir

Reputation: 833

How to hide TR if its values are similar to another TR in jquery?

I want to hide the TR if its first TD value is similar to another TRs TD value.

Eg:

<table>
<tr>
    <td>123</td>
    <td>Example 1</td>
</tr>
<tr>
    <td>789</td>
    <td>Example 2</td>
</tr> 
<tr>
    <td>123</td>
    <td>Example 3</td>
</tr>   
</table>

What I want:

<table>
<tr>
    <td>123</td>
    <td>Example 1</td>
</tr>
<tr>
    <td>789</td>
    <td>Example 2</td>
</tr>    
</table>

Can anybody tell me how to do this?

Any help would be highly appreciated.

Upvotes: 0

Views: 43

Answers (2)

geekbro
geekbro

Reputation: 1323

HTML

<table id="my_table">
 <tr>
<td>123</td>
<td>Example 1</td>
   </tr>
   <tr>
<td>789</td>
<td>Example 2</td>
 </tr> 
  <tr>
<td>123</td>
<td>Example 3</td>
 </tr>   
 </table>

In javascript using jquery

<script>
$("#my_table tr").each(function(){
 var findTxt=$(this).find("td:eq(0)").text();
 $("#my_table tr").each(function(){
   if($(this).find("td:eq(0)").text()==findTxt){
        $(this).remove();
       }
     });
     });
 </script>

Upvotes: 1

adeneo
adeneo

Reputation: 318182

You could create a map of the text in the first TD, and then filter based on that

var trs = $('table tr'),
    map = trs.map(function() { return $(this).find('td:first').text().trim() }).get();

trs.filter(function(i) {
  return map.indexOf( $(this).find('td:first').text().trim() ) !== i;
}).hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>123</td>
    <td>Example 1</td>
  </tr>
  <tr>
    <td>789</td>
    <td>Example 2</td>
  </tr>
  <tr>
    <td>123</td>
    <td>Example 3</td>
  </tr>
</table>

Upvotes: 2

Related Questions