Wednesday, October 15, 2025
HomeLanguagesJavascriptHow to send row data when clicking button using JavaScript ?

How to send row data when clicking button using JavaScript ?

In this article, we will learn how to send and access row data when clicking a button using JavaScript.

The below example contains a simple Bootstrap table. Each table row (<tr>) has an id and a submit button. When the button corresponding to a row is clicked, the row’s data is displayed using an alert. 

Approach: We get the row id of the row whose button was clicked using “event.target.parentNode.parentNode.id”. Here, “event. target” returns the element that triggered the event (clicking the button), which in this case is <input>.  “event.target.parentNode” refers to the parent of <input>, that is <td>.  “event.target.parentNode.parentNode()” refers to the parent of <td>, that is <tr>. Therefore, var rowId stores id of the row whose button was clicked.

Now we want to access the data inside this particular row. All data elements have class=”row-data”. document.getElementById(rowId).querySelectorAll(“.row-data”) ” returns an array of all elements with “row-data” class inside the element (row) whose id is given by rowId. The array is stored in var “data” variable. We can access the data using data[0].innerHTML (name) and so on.

Example: This example shows the above-explained approach.

html




<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet"
          href=
          integrity=
"sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
          crossorigin="anonymous" />
</head>
 
<body>
    <!-- Bootstrap table -->
    <table class="table">
        <tbody>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Name</th>
                <th scope="col">Age</th>
                <th scope="col">Submit</th>
            </tr>
 
            <tr id="1">
                <th scope="row">1</th>
                <td class="row-data">Sara</td>
                <td class="row-data">23</td>
                <td><input type="button" value="submit"
                           onclick="show()" />
                  </td>
            </tr>
 
            <tr id="2">
                <th scope="row">2</th>
                <td class="row-data">John</td>
                <td class="row-data">30</td>
                <td><input type="button" value="submit"
                           onclick="show()" />
                  </td>
            </tr>
 
            <tr id="3">
                <th scope="row">3</th>
                <td class="row-data">Naman</td>
                <td class="row-data">20</td>
                <td><input type="button" value="submit"
                           onclick="show()" />
                  </td>
            </tr>
        </tbody>
    </table>
 
    <script>
        function show() {
            var rowId =
                event.target.parentNode.parentNode.id;
        //this gives id of tr whose button was clicked
            var data =
        document.getElementById(rowId).querySelectorAll(".row-data");
        /*returns array of all elements with
        "row-data" class within the row with given id*/
         
            var name = data[0].innerHTML;
            var age = data[1].innerHTML;
         
            alert("Name: " + name + "\nAge: " + age);
        }
    </script>
</body>
</html>


Output:

Send row data when clicking button

Send row data when clicking button

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11891 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11952 POSTS0 COMMENTS
Shaida Kate Naidoo
6851 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS