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

130
Visualizações
Event not Bubbling to Parent Element from Child Element

I read the article on https://javascript.info/bubbling-and-capturing about Bubbling.

According to the article, the following event on the article tag with id="article" should have bubbled to the <p id="para"></p>, inside which article is nested.

    let para = document.getElementById("para");
    let article = document.getElementById("article");

    /*
    article.addEventListener("click", () => {
        console.log("I'm an article tag!");
    });
    */
    para.addEventListener("click", () => {
        console.log("I'm a p tag!");
    });
        <p id="para">
            <article id="article">Click Me!</article>
        </p>

However, absolutely, nothing happens. The Event listener on article works just fine(when not commented out). Why?

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

0

Your HTML is invalid. An <article> can't be a child of a <p> - check out the rendered HTML:

console.log(document.body.innerHTML);
<p id="para">
  <article id="article">Click Me!</article>
</p>

As a result, the browser ends the <p id="para"> when the <article> tag begins - so they're no longer parent and child, but siblings.

(The <p> tag can only contain phrasing content, which the <article> tag is not)

Make sure your HTML passes validation first.

If you used something else valid - like a <span> - it'd work:

let para = document.getElementById("para");
let article = document.querySelector("span");

/*
article.addEventListener("click", () => {
    console.log("I'm an article tag!");
});
*/
para.addEventListener("click", () => {
  console.log("I'm a p tag!");
});
<p id="para">
  <span id="article">Click Me!</span>
</p>

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