I am trying to create a function on my website so that users can click a "save" button on a particular row of this table that will pull up the innerHtml of the entire row element that they have selected and then Im going to put that info into another page for them as their saved hiking trails.
I have been trying to do this by adding click event listeners to the tables and then accessing the information of the table row through the target.
Does anyone know how I can access the inner html of the whole row and not just the cell that the save button is in?
Here is my html:
<table class="table">
<tr>
<th>Trails</th>
<th>Province</th>
<th>Country</th>
</tr>
<tr>
<td class="tableRow">Lion's Head</td>
<td class="tableRow">Western Cape</td>
<td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
</tr>
<tr>
<td class="tableRow">Pipe Track</td>
<td class="tableRow">Western Cape</td>
<td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
</tr>
<tr>
<td class="tableRow">Skeleton Gorge</td>
<td class="tableRow">Western Cape</td>
<td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
</tr>
<tr>
<td class="tableRow">Table Mountain</td>
<td class="tableRow">Western Cape</td>
<td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
</tr>
<tr>
<td class="tableRow">King Fisher Trail</td>
<td class="tableRow">Western Cape</td>
<td class="tableRow">South Africa<button id="save" button class="save">Save</button></td>
</tr>
<tr>
<td class="tableRow">Robberg Peninsula</td>
<td class="tableRow">Western Cape</td>
<td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
</tr>
<tr>
<td class="tableRow">Diep Walle Forest Walk</td>
<td class="tableRow">Western Cape</td>
<td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
</tr>
</table>
And here is the javascript that I have tried to use to target the row that I need.
const save = document.querySelector(".save");
console.log(save);
save.addEventListener("click", (e) => {
let row = document.getElementsByClassName("tableRow");
console.log(row[1], row[2], row[3]);
});
This javascript is not targeting the right table cells.