+ 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??
3 Respuestas
+ 3
VEDANG THANKS YOU SO MUCH, IT WORK :)
+ 2
also i prefer addEventListener() to the onclick attribute in html because it separates js from html and makes cleaner code