Searching found Event
and CustumEvent
, both can be used to create events, and to control subscriptions, removals and issues of the event you should use EventTarget
, but I did not understand how they work, for example, like me could you create a custom click event?
//Cria um evento de clique personalizado
const myClick = new Event('myClick');
//Alguma coisa para guardar as chamadas de "addEventListener"
//Adiciona um ouvinte do evento personalizado aos elmentos
myElement.addEventListener('myClick', event => console.log(event));
myOtherElement.addEventListener('myClick', event => console.log(event));
//Quando houver um clique na tela
window.addEventListener('click', function(event) {
//Percorre uma lista onde ficaria armazenado os elementos que foram adicionado ouvintes do evento
for(element of elementList) {
//Se o elemento for igual ao elemento principal do clique na janela
if (event.target === element) {
//Dispara o evento customizado de clique
element.dispatchEvent(myClick);
}
}
});
Am I on the right track? How do I save calls to addEventListener
?
The example is exactly the same as the click event that already exists, it's just an example. At the moment I just want to know how to do it in modern browsers, I'm not worried about compatibility