0

How do I get specific value from an array of values in javascript.

For example I have <td>one</td> <td>ten</td> <td>six</td> <td>two</td> How do I only get six without directly using event handlers in each tag

9th Apr 2021, 3:20 PM
Hilary
Hilary - avatar
6 Answers
+ 4
Collect references of the desired elements, then add click event listener to each element window.onload = () => { document.querySelectorAll("td") .forEach(col => { col.addEventListener("click", evt => { console.log(event.target.innerHTML); }); }); }
9th Apr 2021, 3:52 PM
Ipang
+ 5
I presume this is a table row element, so the parent of td is tr tag. We could add a single eventListener to parent tr tag, the event target would be the targeted child, td element. <tr> <td>one</td> <td>ten</td> <td>six</td> <td>two</td> </tr> onload = init; function init() { var tr = document.querySelector("tr"); tr.addEventListener("click", onRowClick, false); } function onRowClick (e) { console.log(e.target.innerHTML) } https://code.sololearn.com/WAJbPBq9wsYk/?ref=app
10th Apr 2021, 12:00 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 3
Something like this ? alert(document.querySelectorAll("td")[2].innerHTML)
9th Apr 2021, 3:24 PM
TOLUENE
TOLUENE - avatar
+ 1
Thanks RIP VIP , your the best 👌
9th Apr 2021, 5:22 PM
Hilary
Hilary - avatar
0
By deleting others🤣🤣
9th Apr 2021, 3:26 PM
RIP VIP
RIP VIP - avatar
0
Thanks Sir Isaac Newton, what I meant is the process should be automatic after mouseclick event
9th Apr 2021, 3:28 PM
Hilary
Hilary - avatar