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

151
Visualizações
checkbox hides problem when we click on it

I have added an click event to the class sec which will show and hide the checkbox.
But when I click on checkbox, then checkbox hides.

let sec = document.querySelector('.sec');
let checkbox = document.querySelector('.switch');

sec.addEventListener('click', ()=>{
  checkbox.classList.toggle("show");
})
.sec{
  background:lightgray;
  padding:10px;
  width:450px;
  cursor:pointer;
  position:relative;
  margin-top:70px;
}

.switch{
  display:none;
  background:gray;
  padding:5px;
  position:absolute;
  bottom : 100%
}
.switch::after {
    content: '';
    position: absolute;
    top: 100%;
    left: calc(50% - 10px);
    background: #88b7d5;
    background: #242334;
    border: 1px solid #3F3E5B; 
    width: 10px;
    height: 10px;
    -webkit-clip-path: polygon(0 0, 100% 0, 50% 100%);
    clip-path: polygon(0 0, 100% 0, 50% 100%);
}
.show{
  display:block;
}
<section class="sec">
  <p>Click me to show popup</p>
  <div class="popup">
    <label class="switch">
      I am checkbox
      <input type="checkbox">
    </label>
</div>
</section>

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

0

The click on the checkbox is also a click on the section because the checkbox is inside the section, so the event listener is toggling the checkbox.

Add an event listener on the checkbox label that uses event.stopPropagation() to prevent the event from bubbling to the container.

let sec = document.querySelector('.sec');
let label = document.querySelector('.switch');

sec.addEventListener('click', () => {
  label.classList.toggle("show");
})

label.addEventListener('click', (event) => {
  event.stopPropagation()
})
.sec {
  background: lightgray;
  padding: 10px;
  width: 450px;
  cursor: pointer;
  position: relative;
  margin-top: 70px;
}

.switch {
  display: none;
  background: gray;
  padding: 5px;
  position: absolute;
  bottom: 100%
}

.switch::after {
  content: '';
  position: absolute;
  top: 100%;
  left: calc(50% - 10px);
  background: #88b7d5;
  background: #242334;
  border: 1px solid #3F3E5B;
  width: 10px;
  height: 10px;
  -webkit-clip-path: polygon(0 0, 100% 0, 50% 100%);
  clip-path: polygon(0 0, 100% 0, 50% 100%);
}

.show {
  display: block;
}
<section class="sec">
  <p>Click me to show popup</p>
  <div class="popup">
    <label class="switch">
      I am checkbox
      <input type="checkbox">
    </label>
  </div>
</section>

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