querySelectorAll() the best JavaScript function in the world!

Is it possible to use querySelectorAll() to select all table cells that have a specific class within a specific row or column?

1 answer

  1. Yes, it is possible to use `querySelectorAll()` to select all table cells that have a specific class within a specific row or column. To do this, you need to combine the row or column selector with the class selector.

    Here's an example:

    ```html
    <table id="myTable">
    <tr>
    <td class="myClass">1</td>
    <td>2</td>
    </tr>
    <tr>
    <td>3</td>
    <td class="myClass">4</td>
    </tr>
    <tr>
    <td class="myClass">5</td>
    <td>6</td>
    </tr>
    </table>
    ```

    Using JavaScript, you can select all table cells with class `myClass` in the first row like this:

    ```javascript
    const firstRow = document.querySelector("#myTable tr:first-child");
    const cells = firstRow.querySelectorAll("td.myClass");
    ```

    And to select all table cells with class `myClass` in the first column, you can do the following:

    ```javascript
    const cells = document.querySelectorAll("#myTable tr td:first-child.myClass");
    ```

    Adjust the index for `:first-child` or use `:nth-child(n)` to target a different row or column.

Related Questions