Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
How to force Event.curentTarget for dispatched events on global listeners in javascript?

I got a question about dispatching events in Javascript.

Here is a little code section providing the intro, which works as expected. The event.currentTarget is the same as if I directly clicked on the button itself.

var button1 = document.querySelector('.button1');
button1.addEventListener('click', function (e) {
    alert(`Button1 was clicked!\nEvent currentTarget: ${e.currentTarget} .${e.currentTarget.className}`);
});

var event = new Event('click');
button1.dispatchEvent(event);
<p>if you load the page a button click will be dispatched</p>

<button class="button1">Button1: If you click me I wil open an alert</button>

<p>Expected Output: "Button1 was clicked! [object HTMLButtonElement] .button1"</p>

Question: Why is different if I have a listener on the document.

Event.currentTarget seems to be the element from where the event was dispatched from, not the element where dispatch was called on.

var button1 = document.querySelector('.button1');
document.addEventListener('click', function (e) {
    alert(`Button1 was clicked!\nEvent.currentTarget: ${e.currentTarget} .${e.currentTarget.className}`);
});

var event = new Event('click');
button1.dispatchEvent(event);
<p>Click anywhere besides the button itself to see the difference!</p>

<button class="button1">Button1: If you click me I wil open an alert</button>

<p>Expected Output: "Button1 was clicked! [object HTMLButtonElement] .button1"</p>
<p>Actual Output: "Button1 was clicked! [object HTMLHtmlElement]"</p>

Is there a way to force the event.currentTarget?

Ideally I want to use a global listener handling multiple purposes and check per event.target if that button was clicked.

So how can I differentiate between any other clicks and the dispatch event for the button while still using a global event listener

One slightly more detailed demo can be found on codepen!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think the actual question is how to make an event triggered bubble up a tree? Use MouseEvent and set it to bubble.

var button1 = document.querySelector('.button1');
document.addEventListener('click', function (e) {
  console.log( e.target.nodeName, e.currentTarget.nodeName);
});

const event = new MouseEvent("click", {
    view: window,
    bubbles: true,
    cancelable: true,
});
button1.dispatchEvent(event);
<button class="button1">Click</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda