0

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 ?

9
  • 2
    .size() should be .length Commented Mar 29, 2017 at 23:59
  • 1
    .length is not .length() Commented Mar 30, 2017 at 0:03
  • You may use .forEach(value => { }) to check value of checkbox and count it s amount under condition Commented Mar 30, 2017 at 0:03
  • OK, I corrected this. By the way, do I have the obligation to use jquery, as I see you edited my post ? Commented Mar 30, 2017 at 0:03
  • 1
    The pure JavaScript way is document.querySelectorAll(".z:checked").length Commented Mar 30, 2017 at 0:07

5 Answers 5

2
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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I'm trying to put this in my original code but I have already something in the onclick event. How can I have two functions in it ?
onclick="fxn1(); fxn2();"
@Guillaume you can call two functions in onclick event. onclick="updateCount(); someFunction();"
@kangsu : I have this already (in an echo) : onclick="document.getElementById(\'radio-'.$ligne['id'].'-2\').checked = false". So I tried this but it doesn't work : onclick="updateCount(); fxn2(document.getElementById(\'radio-'.$ligne['id'].'-2\').checked = false);"
0

Use length

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

1 Comment

Thanks for your answer. Is there something more to do ? It doesn't work.
0
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

2 Comments

Thank you. I'm trying to put this in my original code but I have already something in the onclick event. How can I have two functions in it ?
onclick="fxn1(); fxn2();"
0

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/

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.