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

274
Views
set back counter to 0 if nothing is checked Javascript

I have multiple dropdowns that contain checkboxes and my goal is "on click" to check how many boxes are checked and if it's more than 1 to hide a logo.

I have 2 Problems.

Problem 1:

The counter never sets to 0 if no check box is checked.

Problem 2:

Every time I click it runs through the function multiple times and I end up getting multiple console.logs which is bad and confusing.

function init() {
        var elements = document.getElementsByClassName("filter-multi-select-list-item");

        var myFunction = function () {
            var inputElems = document.getElementsByTagName("input"),
                count = 0;
            for (var ii = 0; ii < inputElems.length; ii++) {
                if (inputElems[ii].type === "checkbox" && inputElems[ii].checked === true) {
                    count++;
                    console.log(count);
                    if (count === 1){
                        console.log("show logo");
                    }else{
                        console.log("hide logo");
                    }
                }
            }}

        for (var i = 0; i < elements.length; i++) {
            elements[i].addEventListener('click', myFunction, false);
        }
    }
<body onload="init()">
   <div class="filter-multi-select-dropdown filter-panel-item-dropdown collapse show" id="filter-manufacturer-707419086" style="">
    <ul class="filter-multi-select-list">
        <li class="filter-multi-select-list-item">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input filter-multi-select-checkbox" data-label="Dockers by Gerli" value="ba4fa0f5f0784bcfa485e359cbe92546" id="ba4fa0f5f0784bcfa485e359cbe92546">
                <label class="filter-multi-select-item-label custom-control-label" for="ba4fa0f5f0784bcfa485e359cbe92546">Dockers by Gerli</label>
            </div>
        </li> 
        <li class="filter-multi-select-list-item">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input filter-multi-select-checkbox" data-   label="Nike" value="d9e943a4c0c2419cbd87d5100d221236" id="d9e943a4c0c2419cbd87d5100d221236">
                <label class="filter-multi-select-item-label custom-control-label" for="d9e943a4c0c2419cbd87d5100d221236">Nike</label>
            </div>
        </li>
    </ul>
    </div>
</body>

Any solution to the mentioned problems that I have is appreciated.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can set the count globally and only listen to the element you clicked. You don't need to go through all the elements to check the count again

The count will be updated according to every click on 1 element

function init() {
  var elements = document.getElementsByClassName("filter-multi-select-list-item");
  var count = 0;
  var myFunction = function(event) {
    //only check the current clicked checkbox
    if (event.target.checked) {
      count++; //if it's ticked, we increase the count
    } else {
      count--; //if it's unticked, we decrease the count
    }
    console.log(count);
    if (count === 1) {
      console.log("show logo");
    } else {
      console.log("hide logo");
    }
  }

  for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener('click', myFunction, false);
  }
}
<body onload="init()">
  <div class="filter-multi-select-dropdown filter-panel-item-dropdown collapse show" id="filter-manufacturer-707419086" style="">
    <ul class="filter-multi-select-list">
      <li class="filter-multi-select-list-item">
        <div class="custom-control custom-checkbox">
          <input type="checkbox" class="custom-control-input filter-multi-select-checkbox" data-label="Dockers by Gerli" value="ba4fa0f5f0784bcfa485e359cbe92546" id="ba4fa0f5f0784bcfa485e359cbe92546">
          <label class="filter-multi-select-item-label custom-control-label" for="ba4fa0f5f0784bcfa485e359cbe92546">Dockers by Gerli</label>
        </div>
      </li>
      <li class="filter-multi-select-list-item">
        <div class="custom-control custom-checkbox">
          <input type="checkbox" class="custom-control-input filter-multi-select-checkbox" data- label="Nike" value="d9e943a4c0c2419cbd87d5100d221236" id="d9e943a4c0c2419cbd87d5100d221236">
          <label class="filter-multi-select-item-label custom-control-label" for="d9e943a4c0c2419cbd87d5100d221236">Nike</label>
        </div>
      </li>
    </ul>
  </div>
</body>

about 4 years ago · Juan Pablo Isaza Report

0

We can use filter() on the nodelist to get only the <input>'s that are checked.

Then we can simplify the condition to:

  • Hide logo the amount of checked <input>'s is not 1:
var inputElems = document.getElementsByTagName("input");
var checked = [ ...inputElems ].filter(e => e.checked).length;
var hideLogo = checked !== 1;
console.log('Should hide logo: ', hideLogo);

var elements = document.getElementsByClassName("filter-multi-select-list-item");

var myFunction = function () {
    var inputElems = document.getElementsByTagName("input");
    var checked = [ ...inputElems ].filter(e => e.checked).length;
    var hideLogo = checked === 0 || checked > 1;
    console.log('Should hide logo: ', hideLogo);
}

for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener('click', myFunction, false);
}
<div class="filter-multi-select-dropdown filter-panel-item-dropdown collapse show" id="filter-manufacturer-707419086" style="">
<ul class="filter-multi-select-list">
    <li class="filter-multi-select-list-item">
        <div class="custom-control custom-checkbox">
            <input type="checkbox" class="custom-control-input filter-multi-select-checkbox" data-label="Dockers by Gerli" value="ba4fa0f5f0784bcfa485e359cbe92546" id="ba4fa0f5f0784bcfa485e359cbe92546">
            <label class="filter-multi-select-item-label custom-control-label" for="ba4fa0f5f0784bcfa485e359cbe92546">Dockers by Gerli</label>
        </div>
    </li> 
    <li class="filter-multi-select-list-item">
        <div class="custom-control custom-checkbox">
            <input type="checkbox" class="custom-control-input filter-multi-select-checkbox" data-   label="Nike" value="d9e943a4c0c2419cbd87d5100d221236" id="d9e943a4c0c2419cbd87d5100d221236">
            <label class="filter-multi-select-item-label custom-control-label" for="d9e943a4c0c2419cbd87d5100d221236">Nike</label>
        </div>
    </li>
</ul>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You could use a querySelectorAll which returned all checked checkboxes, and look at the length property.

var count = document.querySelectorAll("input[type='checkbox']:checked").length;
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!