Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to hide all rows except selected one without knowing complete ID in JavaScript ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given an HTML table with a set number of rows, the task is to hide all the rows except the selected one without knowing its ID using JavaScript.

Approach: Using the querySelectorAll(), forEach(), addEventListener() and filter() methods & the spread operator (…) in native JavaScript. Firstly, we select all the table rows expect the header row using the querySelectorAll() method, passing in the tr:not(:first-child) selector as a string parameter. We iterate through each table row using the forEach() method. Now, for every table row, an event listener is attached to it using the addEventListener() method with the event being the click event.

Inside the event listener, a variable row is declared which stores all the table row elements in the form of an array as the spread operator is used to convert the node list returned from the querySelectorAll() method to an array. We filter certain rows from all rows using the filter() method, passing a parameter element within it. The condition to filter the rows is that if the row is not clicked, we should hide it using the display property which is set to the value of none.

Example: In this example, we are using the above-explained approach.


<!DOCTYPE html>
        body {
            text-align: center;
        h1 {
            color: green;
            font-size: 2.5rem;
        p {
            font-size: 1.5rem;
            font-weight: bold;
        table {
            margin: 0 auto;
        td {
            border: 1px solid black;
            border-collapse: collapse;
        td {
            padding: 0.5rem;
        tr:not(:first-child) {
            cursor: pointer;
        tr:not(:first-child):hover {
            background: green;
            color: white;
    <p>JavaScript - Hide all rows except
       selected one without ID
            <th>Book Name</th>
            <th>Author Name</th>
            <td>The Book Thief</td>
            <td>Markus Zusak</td>
            <td>Historical Fiction</td>
            <td>The Cruel Prince</td>
            <td>Holly Black</td>
            <td>The Silent Patient</td>
            <td>Alex Michaelides</td>
            <td>Psychological Fiction</td>
          // Select all rows except the first one
          "tr:not(:first-child)").forEach(function (e) {
            // Add onClick event listener
            // to selected rows
            e.addEventListener("click", function () {
                // Get all rows except the first one
                var rows =
                var notSelectedRows =
                    rows.filter(function (element) {
                    // Hide the rows that have
                    // not been clicked
                    if (element !== e) {
               = "none";


My Personal Notes arrow_drop_up
Last Updated : 09 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials