1

I am new using jQuery, and I am trying to hide rows from a table like that:

<table border="1" width="100%" id="table1">
        <tr>
            <td style="text-align: center">
            First Row   
            </td>
        </tr>

        <tr>
            <td style="text-align: center">
            Second row  
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
            Thrid row   
            </td>
        </tr>
</table>

I am trying to hide the first and the 3erd row from table, how can I do it? I am trying but I cannot achieve my goal with this code:

$('#table1 tr').hide()
2
  • 2
    $('#table1 tr:first(), #table1 tr:eq(2)').hide(), or ` #table1 tr:last()` if it's always going to be the last row. Commented Jul 24, 2013 at 2:07
  • What is the expected and actual behavior of the code you provide? Commented Jul 24, 2013 at 2:24

1 Answer 1

5

There are numerous ways to do this. Here's one:

$("#table1 tr:even").hide();

Noting that row indexes are zero-based, so the first and third rows are "even". Of course this hides all even rows but your example only has three rows.

To explicitly target individual rows by (zero-based) index:

var $rows = $("#table1 tr");
$rows.eq(0).hide();
$rows.eq(2).hide();

Or, if you have control over the html you can add a class to whichever rows you want to hide and select based on the class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.