Guillaume
Guillaume

Reputation: 352

How to count checked checkbox with Javascript?

I would like to count the checkboxes that are checked and display the count in the Div.

Here is my HTML :

<form name="liste_figurine" method="post">

<input type="checkbox" id="radio-1" class="z" name="chck1[]" onclick="updateCount()" />
<input type="checkbox" id="radio-2" class="z" name="chck2[]" onclick="updateCount()" />

</form>

<div id="y"></div>

Here is my JS :

function updateCount {
    var x = $(".z:checked").length;
    document.getElementById("y").innerHTML = x;
};

Here is an exemple : https://jsfiddle.net/r3todbs6/3/

Sorry, I'm not really used to JS... What is wrong with my code ?


Finally, it's working but I have one last issue : the onclick must contain 2 functions and I don't manage to make them working together.

Now this function works, alone : onclick="updateCount()"

This other function works also, alone : onclick="document.getElementById(\'radio-'.$ligne['id'].'-2\‌​').c‌​hecked = false"

But these two functions doesn't work, together : onclick="updateCount(); fx2(document.getElementById(\'radio-'.$ligne['id'].'-1\').ch‌​ecked = false);"

What is wrong with the third proposition ? Is it a syntax error ?

Upvotes: 0

Views: 10780

Answers (5)

waldalvajr
waldalvajr

Reputation: 1

The answer provided by kangsu is the best answer but here is how I did it.

var form = document.getElementById('form');
var checkBoxes = $(form).children('.checkbox');
var count = 0;

var divBoxesChecked = document.getElementById('boxesChecked');
divBoxesChecked.innerHTML = 0;

$(checkBoxes).on('click', function() {
  $.each(checkBoxes, function(i) {
    if (checkBoxes[i].checked) {
      count++;
    }
  });
  console.log(count);
  divBoxesChecked.innerHTML = count;
  count = 0;
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<form id="form" method="post">

  <input type="checkbox" id="box_01" class="checkbox" name="box_01" />
  <label>Box 1</label>

  <input type="checkbox" id="box_02" class="checkbox" name="box_02" />
  <label>Box 2</label>

  <input type="checkbox" id="box_03" class="checkbox" name="box_03" />
  <label>Box 3</label>

</form>

<div id="boxesChecked"></div>

Basically, every time one of the boxes is clicked it counts how many boxes are checked in total and prints that out to the div and the console, then it resets the counter to zero since it re-counts all of the checked boxes every time you click on one.

Upvotes: 0

X. Liu
X. Liu

Reputation: 1110

There are three issues for your Fiddle code:

  • As others have mentioned, it should be length, not size().
  • You should change the JavaScript setting -> Load type to "No wrap - in <head>". Otherwise, updateCount is not defined in global scope.
  • You need to configure Fiddle to use jQuery if you use your original code.

A working example: https://jsfiddle.net/c5ez4w7y/

Upvotes: 0

kangsu
kangsu

Reputation: 226

window.updateCount = function() {
    var x = $(".z:checked").length;
    document.getElementById("y").innerHTML = x;
};

https://jsfiddle.net/73yfczcj/

try this. you have to use jquery in javascript setting.

Upvotes: 2

Abhijeet Ahuja
Abhijeet Ahuja

Reputation: 5950

function updateCount {
    var x = document.querySelector('.z:checked').length;
    document.getElementById("y").innerHTML = x;
};

try this fuction if you don't want to use jQuery

Upvotes: 0

Robert I
Robert I

Reputation: 1527

Use length

function updateCount(){
    var x = $("input:checked").length;
    $("#y").html(x);
};

Upvotes: 0

Related Questions