Skip to content
Related Articles
Open in App
Not now

Related Articles

How to send row data when clicking button using JavaScript ?

Improve Article
Save Article
  • Last Updated : 17 Jan, 2023
Improve Article
Save Article

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 “”. Here, “event. target” returns the element that triggered the event (clicking the button), which in this case is <input>.  “” refers to the 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.

Example: This example shows the above-explained approach.


<!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" value="submit" 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" value="submit" 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" value="submit" 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);


Send row data when clicking button

Send row data when clicking button

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!