How to onClick to many elements | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 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 Respostas
+ 3
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