Languages

JavaScript - simulate mouse click on multiple elements?

0 points
Asked by:
Jacob
532

Can you tell me how to simulate mouse click on multiple div elements using JavaScript?

Let's say I have three div elements:

I want a for loop that loops through all my-elements and fires their onclick event.

1 answer
0 points
Answered by:
Admin Dirask Community
4380

You can use querySelectorAll() to get handles to all the elements and then inside a for loop execute click() method on each element.

Practical example:

 

References

  1. HTMLElement.click() - Web APIs | MDN
  2. Document.querySelectorAll() - Web APIs | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join