How to onClick to many elements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to onClick to many elements

If i have 100 paragraphs. And each paragraph has different text in it. I want to make program that if we tap on specific paragraph it will alert text inside that paragraph that we have clicked. For example, i write 100 paragraphs like this : <p>Fish</p> <p>Cat</p> <p>Dog</p> <p>Sheep</p> ..... ..... <p>Cow</p> And when i tap first paragraph it will alert "Fish", When i tap second paragraph it will alert "Cat", when i tap the last paragraph it will alert "Cow" and so on. I have tried to use for loops but its not work. Can someone help me??

5th Sep 2019, 9:25 AM
Bee David
Bee David - avatar
3 Answers
+ 3
VEDANG THANKS YOU SO MUCH, IT WORK :)
5th Sep 2019, 9:47 AM
Bee David
Bee David - avatar
+ 2
also i prefer addEventListener() to the onclick attribute in html because it separates js from html and makes cleaner code
7th Sep 2019, 7:07 AM
kay cruz
kay cruz - avatar