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

376
Visualizações
Why do all elements disappear with "*" selector?

code picture

I'm learning JQuery through w3school. And I came across this example. From my understanding, $("*").dblclick() bind the double click event to each element in the document (each element has its own event and they are seperated).

And, I expect that, after double clicking a <p> element, only the one clicked will disappear. However, when I click a <p> element, every <p> element disappears. May I know how is my understanding wrong?

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

0

"*" selects ALL elements, including <body> and <html> (https://api.jquery.com/all-selector/). So, if you double click one of your paragraphs, the event will be triggered three times, one for <p>, one for <body> and one for <html> generating this "undesired" behavior. If one of your <p> was inside, lets say, a <div>, the event would be triggered four times. One way to prevent this is to stop the event propagation.

$( function() {
    $( "*" ).dblclick( function( event ){
        console.log( event.target.innerHTML );
        console.log( event.target.parentElement.tagName );
        $(this).hide();
        event.stopPropagation();
    });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<p>aaaaaa</p>
<p>bbbbbb</p>
<p>cccccc</p>
<div>
    <p>dddddd</p>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

👋

That's because the * in $("*").dblclick() is a selector as explained in W3Schools.

To which * means "all the elements", referencing the DOM structure of HTML, <body> is also an element, and JQuery is indeed appending the event on it as well, so when you say "when I click a <p> element", what you should actually mean is _"when I click the <body> element".

That is because the <body> element is hierarchically positioned higher than the p elements, and the event is being triggered on that element because it takes precedence over the p which body contains.

So, your code is working absolutely fine. But if you wanted, for example, that "after double clicking a <p> element, only the one clicked will disappear", maybe your code should look something like this:

$("p").dblclick()

Which means, find all p elements and bind this event and will comply with your expectations.

Hope you let me know what happened when you tried it out! Happy coding!

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