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

105
Visualizações
Working with event handlers in Javascript

const divs_demo = document.querySelectorAll('.common');
const display_texts = document.querySelectorAll('.text');



for(let div_demo of divs_demo){
    div_demo.addEventListener('click', ()=>{
        for(let display_text of display_texts){
            display_text.style.display = 'block';
        };
    });
};
.text{
    display: none;
}
.common{
    border: 1px solid;
}
<body>
    
    <div class="common">
        <h1>Text one</h1>
        <p class="text">Hello</p>
    </div>
    <div class="common">
        <h1>Text two</h1>
        <p class="text">Good morning</p>
    </div>
    <div class="common">
        <h1>Text three</h1>
        <p class="text">Lets code</p>
    </div>

    <script src="app.js"></script>
</body>

Am working on with event handlers in JavaScript, however, have encountered a problem on my way, on the above code, I have got access to all the div elements in my html, and some hidden text which I want it to be displayed once the event handler which I have passed to the divs is performed, but once one div is clicked, it displays the all the text including the one on the other divs, but my target is to only display the text on the div which is clicked. Is there a way to do that without repeating the same code to every div?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Do you mean this?

const divs_demo = document.querySelectorAll('.common');
const display_texts = document.querySelectorAll('.text');

for (let div_demo of divs_demo) {
  div_demo.addEventListener('click', () => {
    div_demo.querySelector(".text").style.display = 'block';
  });
};
.text{
    display: none;
}
.common{
    border: 1px solid;
}
<body>
    
    <div class="common">
        <h1>Text one</h1>
        <p class="text">Hello</p>
    </div>
    <div class="common">
        <h1>Text two</h1>
        <p class="text">Good morning</p>
    </div>
    <div class="common">
        <h1>Text three</h1>
        <p class="text">Lets code</p>
    </div>

    <script src="app.js"></script>
</body>

about 4 years ago · Santiago Trujillo Relatório

0

Is there a way to do that without repeating the same code to every div?

Yes there's which is event-delegation

document.addEventListener('click',(e)=>{
  e.target.parentElement.querySelector('.text').style.display = 'block';
})
.text {
  display: none;
}

.common {
  border: 1px solid;
}
<body>

  <div class="common">
    <h1>Text one</h1>
    <p class="text">Hello</p>
  </div>
  <div class="common">
    <h1>Text two</h1>
    <p class="text">Good morning</p>
  </div>
  <div class="common">
    <h1>Text three</h1>
    <p class="text">Lets code</p>
  </div>

  <script src="app.js"></script>
</body>

about 4 years ago · Santiago Trujillo Relatório

0

The displayed texts are children of the divs demo, so you can simply get the .text child of the div clicked. Here's how I would do it.

const divsDemo = document.querySelectorAll('.common');
    
for (let divDemo of divsDemo) {
  divDemo.addEventListener('click', () => {
    const clickedText = divDemo.querySelector('.text');
    clickedText.style.display = 'block';
  });
}
.text{
    display: none;
}
.common{
    border: 1px solid;
}
<body>
    <div class="common">
        <h1>Text one</h1>
        <p class="text">Hello</p>
    </div>
    <div class="common">
        <h1>Text two</h1>
        <p class="text">Good morning</p>
    </div>
    <div class="common">
        <h1>Text three</h1>
        <p class="text">Lets code</p>
    </div>

    <script src="app.js"></script>
</body>

NOTE: It's highly recommendable that you respect style standards of the language that you're coding. In this case you should use camelCase naming for variables.
NOTE2: Instead of modifying the style, i would recommend you to use utility classes such as:

.invisible {
    display: none
}

And then appending or removing this class from the element that you want to disappear

about 4 years ago · Santiago Trujillo 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