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:
- How to change the button color after clicking it using AngularJS ?
- How to avoid dropdown menu to close menu items on clicking inside ?
- How to make a Bootstrap 4 accordion collapse when clicking the whole header div ?
- How to send data of HTML form directly to JSON file?
- How to send an email using PHPMailer ?
- How to configure XAMPP to send mail from localhost using PHP ?
- How to send email with Nodemailer using Gmail account in Node.js ?
- How to Send Email using Mailgun API in Node.js ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.