In JavaScript we can trigger events associated with elements.
Suppose we have this code:
const btn = document.querySelector('#btn');
btn.addEventListener('click', () => {
console.log('Clicked');
}, false);
JavaScript has the dispatchEvent()
method which takes an instance of the Event
object as an argument. In the constructor of this object we must specify the event we want to trigger:
const clickEvt = new Event('click');
btn.dispatchEvent(clickEvt); // 'Clicked'