How do I get specific value from an array of values in javascript. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок

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 - avatar
6 ответов
+ 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(; }); }); }
9th Apr 2021, 3:52 PM
+ 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( }
10th Apr 2021, 12:00 AM
Calviղ - avatar
+ 3
Something like this ? alert(document.querySelectorAll("td")[2].innerHTML)
9th Apr 2021, 3:24 PM
TOLUENE - avatar
+ 1
Thanks RIP VIP , your the best 👌
9th Apr 2021, 5:22 PM
Hilary - avatar
By deleting others🤣🤣
9th Apr 2021, 3:26 PM
RIP VIP - avatar
Thanks Sir Isaac Newton, what I meant is the process should be automatic after mouseclick event
9th Apr 2021, 3:28 PM
Hilary - avatar