onclick alerts inside loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

onclick alerts inside loops

https://code.sololearn.com/WY3YSou8oqRK/#js On line 41 of js I try to add an onclick property to each created <td> while passing through a for loop. the onclick property calls a function that alerts the info in an array. The alert displays the correct info, but it does so onload, and the <td>'s are not clickable. How do i create <td>'s that display info when clicked.

27th Feb 2019, 7:55 PM
Zachary Shojinaga
Zachary Shojinaga - avatar
1 Answer
What you need is this: https://developer.mozilla.org/es/docs/Web/API/EventTarget/addEventListener element.addEventListener("click", myFunction); or element.addEventListener("click", function() { // stuff to do } });
27th Feb 2019, 10:59 PM