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

289
Visualizações
One checkbox selects another JS

I have a list with checkboxes, and i need so when one is selected -> it selects one specific checkbox. The code i got only works once.

let checkbox = document.querySelectorAll('.filter__check'); //All the checkboxes
let available = document.querySelector('[name="filter.v.availability"]'); // One that i need be checked

for (let i = 0; i < checkbox.length; i++) {
    checkbox[i].onclick = function(){
        if(checkbox[i].name != "filter.v.availability" && available.checked == false) {
            available.checked = true;
        }
    }
}

Can't understand why it only applies on first click.

enter image description here

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

0

I think using the change event would fit better here.

Also make sure you check if the clicked box is checked so that it wont run when a box is deselected.

let checkbox = document.querySelectorAll('.filter__check'); //All the checkboxes
let available = document.querySelector('[name="filter.v.availability"]'); // One that i need be checked

for (let i = 0; i < checkbox.length; i++) {
    checkbox[i].onchange = function() {
        if(checkbox[i].name !== "filter.v.availability" && checkbox[i].checked) {
            available.checked = true;
        }
    }
}
<ol>
  <li><input type="checkbox" class="filter__check"></li>
  <li><input type="checkbox" class="filter__check"></li>
  <li><input type="checkbox" class="filter__check"></li>
  <li><input type="checkbox" class="filter__check"></li>
</ol>
<input type="checkbox" class="filter__check" name="filter.v.availability">

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