Javascript count checked checkbox
Asked Answered
T

3

4

I know that may have a few similar questions @stackoverflow, but I didnt found any solution to my problem yet :s

<?php
while($rowVideo = mysql_fetch_array($ResultQueryVideo))
{
?>
<input type="checkbox" name = "checkbox-1[]" class="checkbox" value ="<?php echo $rowVideo['idVideo'] ?>" /> <?php....some code...

This results a few checkbox's, the same number as idVideo..thats the point.

Now, before the submit, I need to be sure that at least one checkbox is checked. But i was not sucesseful :x

function isCountCheck (helperMsg) {
    var chkCnt = 0;
    var Frm = document.forms[0];
    var dLen = Frm.length - 1;
    for (i=0;i<=dLen;i++) {
        if(document.form1.["checkbox-1[]"].checked) chkCnt++;
    }

    if (chkCnt>0) {
        return true;
    } else {
        alert(helperMsg);
        return false;
    }
}

Extra details: form name = "form1"

Can you guide me a litle ? Thanks

EDIT:

function isCountCheck(){
    if($("input[type=checkbox]:checked").length > 0)
    {
    return true;
    }
    else
    {
    alert('Invalid');
    return false;
    }
}

But still not working..even that alert is shown..

Tso answered 7/8, 2012 at 3:1 Comment(2)
what is document.addemisao?Rounce
The code from your update uses jQuery - have you included the jquery.js library in your page?Deicide
D
3

The main problem is that you're not using the i index within the loop to reference the individual checkboxes, and you've got a . just before the [ which is a syntax error. So change:

if(document.form1.["checkbox-1[]"].checked) chkCnt++;

To:

if(document.form1["checkbox-1[]"][i].checked) chkCnt++;

But you can neaten up the function somewhat as follows:

function isCountCheck(helperMsg) {
    var i, dLen = document.form1["checkbox-1[]"].length;
    // if the length property is undefined there is only one checkbox
    if (typeof dLen === "undefined") {
        if (document.form1["checkbox-1[]"].checked) return true;
    }
    else {
        for (i = 0; i < dLen; i++) {
            if (document.form1["checkbox-1[]"][i].checked) return true;
        }
    }
    alert(helperMsg);
    return false;
}

Demo: http://jsfiddle.net/nnnnnn/ZjK3w/1/

Or just loop through all inputs in the form, checking the type (and/or the name) of each one:

function isCountCheck(helperMsg) {
    var i, len, inputs = document.form1.getElementsByTagName("input");
    for (i = 0, len = inputs.length; i < len; i++) {
        if (inputs[i].type === "checkbox"
            && inputs[i].checked)
            return true;
    }
    alert(helperMsg);
    return false;
}

Demo: http://jsfiddle.net/nnnnnn/ZjK3w/2/

Deicide answered 7/8, 2012 at 3:43 Comment(5)
the thing is, I have only one <input type="checkbox"> and because of while, it may appear 3 ou 5 our more checkbox, depends of number of idVideo rows I got.Tso
Like this: IF I have 3 Videos - 3 checkbox appear. IF I have 1 video - just 1 checkbx is show. And that is working fine. So i cant solve my problem the way You suggest. Thanks anywayTso
Hi, I think it may worked :) Thanks. And thanks stackoverflow!Tso
From an old-time but long retired programmer, I'm trying to count checks too so I hope not too stupid a question. Where is the function actually being called? In the form? In each checkbox?Champion
@DonP - The OP doesn't say exactly where they call the function from, but I assume probably from an on submit handler given they want to validate before submission. In my little jsfiddle demos I called it from a click handler on a button.Deicide
S
2

Simplest solution:

var form = document.forms[0]; // your form element (whatever)
var checkedElms = form.querySelectorAll(':checked').length;

No need for jQuery. Supported down to IE8. use polyfill for older browsers if you wish.

Somewhat answered 24/3, 2014 at 21:20 Comment(0)
A
0

Use Jquery:

function isCountCheck(helperMsg){
    if($("input[type=checkbox]:checked").length > 0)
        return true;
    alert(helperMsg);
    return false;
}
Asparagine answered 7/8, 2012 at 3:16 Comment(1)
It works dont worry =). You must have forgotten to include the JQuery library in your demo. I added my function above to your JSFiddlle: jsfiddle.net/cHxFJ/1Asparagine

© 2022 - 2024 — McMap. All rights reserved.