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

146
Visualizações
Why compare element to this keyword?

I am looking at the following link which is a part of event delegation documentation.

In the link, we have the followig

let table = document.getElementById('bagua-table');

    let selectedTd;

    table.onclick = function(event) {
      let target = event.target;

      while (target != this) {
        if (target.tagName == 'TD') {
          highlight(target);
          return;
        }
        target = target.parentNode;
      }
    }

I am just unable to understand why the following while (target != this). I understand that the this keyword here refers to the entire table since that is the object in play but why write a loop in this manner?

Maybe my understanding of this is not as deep. If someone could please clarify.

Thanks

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

0

What this does is, the target starts at the event's target - the innermost element the event was dispatched to. Then it searches the target, then its ancestor, then its ancestor, and so on. The first one that matches the condition target.tagname == 'TD', if any, will result in highlight being called and the loop ending. If no elements match, then the loop will continue until the target is this - the table - and will end at that point.

But this is pretty confusing code. Using .closest would be much easier, since it will find the first ancestor matching a selector.

table.onclick = (event) => {
  const td = event.target.closest('td');
  if (td) highlight(td);
};

If nested tables are a possibility, and you don't want the event to go outward past the table in question, include the table in the selector string:

table.onclick = (event) => {
  const td = event.target.closest('#bagua-table td');
  if (td) highlight(td);
};
about 4 years ago · Juan Pablo Isaza Relatório

0

In an event listener, this is equivalent to event.currentTarget, which is the element that the listener was added to (the table).

event.target is the nested element that was actually clicked on.

So the loop is walking up the DOM hierarchy (using target.parentNode) from the clicked element until it reaches the table that the listener was added to.

If it reaches a TD element along the way, it highlights that element and returns, which breaks out of the loop.

It's roughly equivalent to:

highlight(event.target.closest("TD"));
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