Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to trigger an event (together) in JavaScript?

i have created an EventListener for each button but i have button generation all and i need to call all events at once.I tried to put everything inside a function, but I don’t know how to activate all events.
I would be very grateful for any help

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can just call HTMLElement.click() on everyelement to simulate the click on every element. You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

buttonAll.addEventListener('click', ()=> {
    nameButton.click();
    balanceButton.click();
    ageButton.click();
    documentButton.click();
    levelButton.click();
    buttonClear.click();
})

if the list can grow then you might want to group the elements in an array to keep the function short

let buttonAllTargets = [
    nameButton,
    balanceButton,
    ageButton,
    documentButton,
    levelButton,
    buttonClear
];

buttonAllTargets.forEach(element => element.click());
buttonAll.addEventListener('click', ()=> {
    buttonAllTargets.forEach(element => element.click());
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading