3

I have a lot of rows and I want to hide some tr that doesn't have a specific class.

Example:

<tr id="game-22590" class="game">
    <td class="pos left-edge">
        <div>2</div>
    </td>
    <td class="cost">
        <input class="dollars-paid uncorrected" type="text" value="19,99" tabindex="4">
    </td>
</tr>
<tr id="game-22591" class="game">
    <td class="pos left-edge">
        <div>3</div>
    </td>
    <td class="cost">
        <input class="dollars-paid" type="text" value="23,99" tabindex="4">
    </td>
</tr>

The td.cost has an input with a class or two. I want to hide those rows that doesn't have the uncorrected class.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
apeiron
  • 71
  • 1
  • 7

3 Answers3

2

Use .filter() to selecting rows has uncorrected class.

$("tr.game").filter(function(){
    return $(this).find("input.uncorrected").length == 0;
}).hide();

$("tr.game").filter(function(){
    return $(this).find("input.uncorrected").length == 0;
}).hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr id="game-22590" class="game">
    <td class="pos left-edge">
      <div>2</div>
    </td>
    <td class="cost">
      <input class="dollars-paid uncorrected" type="text" value="19,99" tabindex="4">
    </td>
  </tr>
  <tr id="game-22591" class="game">
    <td class="pos left-edge">
      <div>3</div>
    </td>
    <td class="cost">
      <input class="dollars-paid" type="text" value="23,99" tabindex="4">
    </td>
  </tr>
</table>
Mohammad
  • 21,175
  • 15
  • 55
  • 84
1

You can do it with closest() method jQuery... please check this solution

$(".dollars-paid").not(".uncorrected").closest(".game").hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<table>
  <tr id="game-22590" class="game">
    <td class="pos left-edge">
        <div>2</div>
    </td>
    <td class="cost">
        <input class="dollars-paid uncorrected" type="text" value="19,99" tabindex="4">
    </td>
</tr>
<tr id="game-22591" class="game">
    <td class="pos left-edge">
        <div>3</div>
    </td>
    <td class="cost">
        <input class="dollars-paid" type="text" value="23,99" tabindex="4">
    </td>
</tr>

<tr id="game-22592" class="game">
    <td class="pos left-edge">
        <div>3</div>
    </td>
    <td class="cost">
        <input class="dollars-paid" type="text" value="25,99" tabindex="4">
    </td>
</tr>
  </table>
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
1
$("table tr.game").each(function(){
    var self=$(this);
    if(!self.find("input").hasClass("uncorrected")){
        self.hide();
    }
});

Working DEMO

Satinder singh
  • 10,100
  • 16
  • 60
  • 102