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 “event.target.parentNode.parentNode.id”. Here, “event.targent” returns the element that triggered the event (clicking the button), which in this case is <input>. “event.target.parentNode” refers to 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.innerHTML (name) and so on.
Output before button click:
Output after clicking 3rd button: