最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to do this hide all rows except one row in jQuery? - Stack Overflow

matteradmin3PV0评论

How to hide All rows except current (clicked Row) using jQuery?

I want to hide all rows when i click the particular row except current one row.

<table>
@foreach (var item in Model)
    {
        idValue++;

        <tr class="tableRow" id="@idValue">
            <td class="master"  id="@item.Batch_Seq">
                <a href= "#" >@(item.Batch_Name)></a>
            </td>
            <td>
                @(item.Present)
            </td>
            <td>
                @(item.Absent)
            </td>
            <td>
                @(item.HalfDay)
            </td>
            <td>
                @(item.Batch_count)
            </td>
        </tr>

    }
</table>

I tried like this

$('.tableRow').hide();
$(this).closest('tr:not(.tableRow)').hide();

Could anyone help me?

How to hide All rows except current (clicked Row) using jQuery?

I want to hide all rows when i click the particular row except current one row.

<table>
@foreach (var item in Model)
    {
        idValue++;

        <tr class="tableRow" id="@idValue">
            <td class="master"  id="@item.Batch_Seq">
                <a href= "#" >@(item.Batch_Name)></a>
            </td>
            <td>
                @(item.Present)
            </td>
            <td>
                @(item.Absent)
            </td>
            <td>
                @(item.HalfDay)
            </td>
            <td>
                @(item.Batch_count)
            </td>
        </tr>

    }
</table>

I tried like this

$('.tableRow').hide();
$(this).closest('tr:not(.tableRow)').hide();

Could anyone help me?

Share edited Feb 3, 2016 at 17:27 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jun 18, 2013 at 7:36 Kanagaraj VadivelKanagaraj Vadivel 4732 gold badges7 silver badges14 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

You seem to want this :

$('.tableRow').click(function(){
     $('.tableRow').hide(); // hide all rows
     $(this).show(); // show the clicked one
});

Note that the user can't notice the row was hidden then shown : the screen isn't repaint until the event handler ends.

Use .not() to select all elements with class tableRow except for the one that was clicked(this)

$('tr.tableRow').click(function() {
    $('tr.tableRow').not(this).hide();
});

You can use not() to hide everything that isn't "this":

 $('table tr.tableRow').click(function(){
        $('table tr').not(this).hide(); // hide everything that isn't "this"
    });

or you can use .siblings() to hide the siblings of the clicked row

$('table tr').click(function(){
    $(this).siblings().hide(); // hide the siblings of the clicked row
});

Working example : http://jsfiddle/ouadie/U7eUR/

Post a comment

comment list (0)

  1. No comments so far