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 alert. 


We get the row id of the row whose button was clicked using “”. Here, “event.targent” returns the element that triggered the event (clicking the button), which in this case is <input>.  “” refers to parent of <input>, that is <td>.  “” 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.






<!DOCTYPE html>
        <link rel="stylesheet"
              crossorigin="anonymous" />
        <!-- Bootstrap table -->
        <table class="table">
                    <th scope="col">#</th>
                    <th scope="col">Name</th>
                    <th scope="col">Age</th>
                    <th scope="col">Submit</th>
                <tr id="1">
                    <th scope="row">1</th>
                    <td class="row-data">Sara</td>
                    <td class="row-data">23</td>
                    <td><input type="button" 
                               onclick="show()" /></td>
                <tr id="2">
                    <th scope="row">2</th>
                    <td class="row-data">John</td>
                    <td class="row-data">30</td>
                    <td><input type="button" 
                               onclick="show()" /></td>
                <tr id="3">
                    <th scope="row">3</th>
                    <td class="row-data">Naman</td>
                    <td class="row-data">20</td>
                    <td><input type="button" 
                               onclick="show()" /></td>
            function show() {
                var rowId = 
              //this gives id of tr whose button was clicked
                var 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);


Output before button click:

Output after clicking 3rd button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.