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

207
Visualizações
(Javascript) Why have to click multiple times for buttons to work?

simple code:

-------------css--------------
<style>
 .table{
   display: block;
 }
 .row{
    position: relative;
    margin-bottom: 45px;
 }
 .learn{
    font-size: 12px;
    position: absolute;
    top: 30px;
    left: 0;
    z-index:-1;
    text-align: left;
    display: inline;
 }
 .row input{
    position: absolute;
    top: 30px;
    z-index: 1;
    right: 0;
    font-size: 15px;
    padding: 7px 5px;
    border-radius: 10px;
    background: transparent;
}
</style>

-----------------html-------------------
<div class="table">
    <div class="row">
         <div class="thead">Regular Polish</div>
         <div class="td">$20</div>
         <span class="learn">Trimming, shapping, culticle removing, Gel polish put on</span>
         <input class="showBut" type="submit" onclick="showLearn();" value="Learn more"></input>
     </div>
     <div class="row">
     <div class="thead">Gel Polish</div>
     <div class="td">$30</div>
     <span class="learn">Trimming, shapping, culticle removing, Gel polish put on</span>
     <input class="showBut" type="submit"  onclick="showLearn();" value="Learn more"></input>
</div>


----------------script---------------

<script>
 let learn = document.querySelectorAll('.learn')
 let showBut = document.querySelectorAll('.showBut');
 function showLearn(){
    for (const [index, i ] of showBut.entries()){
       i.addEventListener('click', (e) => {
         learn[index].style.display = 
         (learn[index].style.display === 'none') ?
         'inline' : 'none';
        }
    }
 };
</script>

Hi y'all. This is my code. What i am trying to do is: if I click whatever button, the span "above" it will display inline. But I have to click multiple times for the button to work. Please help! Thank you so much!

Or if you guys have any idea to work with multiple buttons with the same event listener and event handler, it'd be great 👍. thank you again

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

0

document.querySelectorAll('.span').forEach((ele) => {
  ele.addEventListener('click', (e) => {
    ele.classList.toggle('red');
  })
})
.span {
  font-size: 16px;
  cursor: pointer;
  background: yellow;
  color: #fff;
}

.red {
  background-color: red;
}
<span class="span">Red or Yellow</span>
<span class="span">Red or Yellow</span>

I hope this will give you some ideas. Good luck

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