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

324
Visualizações
an element event to display none, in an element event to display flex,

I Want to make a button to display a list of elements, and in the list, there is another button to close or display none of the lists but I don't know why it's not working

let btn1 = document.querySelector('#btn1');
let btn2 = document.querySelector('#btn2');
let article = document.querySelector('article');


btn1.addEventListener('click', (e) => {
    article.style.display = 'flex';
})


btn2.addEventListener('click', (e) => {
    article.style.display = 'none';
})
div {
    position: relative;
    width: 100px;
    height: 100px;
    background-color: white;
}

article {
    position: absolute;
    left: 100px;
    display: none;
    flex-direction: column;
}
p {
    color: red;
}

span {
    position: absolute;
    right: -100px;
    color: white;
}
<div id="btn1">
        <article>
            <p>hello world</p>
            <p>hello world</p>
            <p>hello world</p>
            <span id="btn2">btn2</span>
        </article>
    </div>

strong text

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

0

There's a subtle thing happening in your scenario. The divs listening to the click event are nested so that the click event "bubbles" and gets propagated to the parent producing a scenario where both handlers get called for the same click -> hence your element is getting hidden and immediately after showed again.

To prevent that to happen I called event.stopPropagation() in the function handling the click event for the child element.

https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

let btn1 = document.querySelector('#btn1');
let btn2 = document.querySelector('#btn2');
let article = document.querySelector('article');


btn1.addEventListener('click', (e) => {
    article.style.display = 'flex';
})


btn2.addEventListener('click', (e) => {
    article.style.display = 'none';
    event.stopPropagation();
})
div#btn1{
  border: solid 1px red;
  cursor: pointer;
}


div {
    position: relative;
    width: 100px;
    height: 100px;
    background-color: white;
    
    border: solid 1px black;
}

article {
    position: absolute;
    left: 10px;
    display: none;
    flex-direction: column;
    
    border: solid 1px black;
}
p {
    color: red;
}

span {
    position: absolute;
    cursor: pointer;
    /*
    right: -100px;
    color: white;*/
}
<div id="btn1">
    <article>
        <p>hello world</p>
        <p>hello world</p>
        <p>hello world</p>
        <span id="btn2">Click Here</span>
    </article>
</div>

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