Search code examples
javascriptjqueryhtmlhtml-tableclassname

How to hide table rows based on child class name using jquery?


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.


Solution

  • 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>