Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!