1

I have a problem in my javascript. All I want to do is if my user doesnt select any checkbox out given 10 checkboxes then I want to give him an alert else submit the form. I have written a js code but thats not working even if checkbox is checked. Please point me out my mistake. Heres my code.

window.onload = function() {

    document.getElementById('submitbtn').onclick = function() {
                    var count = 0;
                    if(document.getElementById("pid1").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid2").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid3").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid4").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid5").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid6").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid7").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid8").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid9").checked == "checked")
                    {
                        count++;
                    }
                    if(document.getElementById("pid10").checked == "checked")
                    {
                        count++;
                    }
                    alert(count);
                    if(count == 0){
                    alert("Please select atleast 1 product");
                    return false;}
                    else{
                        document.getElementById("formreturn").submit();
                        return false;}
                };
    };
10
  • 2
    if (document.getElementById("pid8").checked) { .. Commented Nov 28, 2014 at 8:06
  • Could you post your .html? Commented Nov 28, 2014 at 8:08
  • 1
    a checkbox element's .checked property is either true or false, not "checked" Commented Nov 28, 2014 at 8:10
  • @ArCiGo My html file doesnt have any problem and checkbox are also right. The same js code has worked for first time but after lil bit changes it didnt work. And it always show count = 0 even if checkboxes are checked. Commented Nov 28, 2014 at 8:12
  • 1
    @MukulAgr Check the answers of abhitalks, gabereal and AKX, are the correct way. Commented Nov 28, 2014 at 8:16

4 Answers 4

4

You should check the checked property for truthy/falsy, because the checked property is a boolean. Like this:

if (document.getElementById("pid8").checked) { ..

Even in HTML you don't need to assign the string "checked" to the "checked" attribute. Only the presence of this attribute is enough.

You can re-write this:

<input type="checkbox" checked="checked" />

as this:

<input type="checkbox" checked />

In fact you can assign anything to it and it will be ignored and treated as presence.

This will also work:

<input type="checkbox" checked="DoNotCheck" />

I see that you are trying to count the number of checked checkboxes. You could do that easily by using a querySelectorAll.

See this Snippet:

document.getElementById('btn').onclick = function() {
    var l = document.querySelectorAll("input[type=checkbox]:checked").length;
    document.getElementById('result').innerText = l;
}
<input type="checkbox" /><input type="checkbox" /><input type="checkbox" />
<input type="checkbox" /><input type="checkbox" /><input type="checkbox" />
<hr />
<input id="btn" type="button" value="Go" />
<p id="result" ></p>

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

Comments

2

Your answer: use checked attribute, it is a boolean, so you don't need to compare it to 'checked'.

Secondly: rewrite it, so there is less code :)

        var count = 0;
        for (var index = 1; index<= 10; index++){
           if(document.getElementById("pid" + index).checked){
                    count++;
           }
        }

1 Comment

Nitpick: use checked attribute: No. We use checked property. Attribute doesn't change. Property does.
2
<form action="some.php" id="myForm" method="POST">
    <input type="checkbox" name="some1" value="" id="some1">
    <label for="some1">title of checkbox 1</label>
    <input type="checkbox" name="some2" value="" id="some2">
    <label for="some2">title of checkbox 2</label>
    <input type="checkbox" name="some3" value="" id="some3">
    <label for="some3">title of checkbox 3</label>
</form>


$(function(){

    var form = $("#myForm");

    form.submit(function(e){

       if(form.find("input[type=checkbox]:checked").length > 0){

       } else{

       }

    });

});

Comments

1

Assuming pidX are inputs of type checkbox, you don't need to compare .checked to "checked".

Also, use a loop...

window.onload = function() {
    document.getElementById('submitbtn').onclick = function() {
        var count = 0;
        for(var i = 1; i <= 10; i++) {
            if (document.getElementById("pid" + i).checked) {
                count++;
            }
        }
        alert(count);
        if (count == 0) {
            alert("Please select at least 1 product");
            return false;
        }
        document.getElementById("formreturn").submit();
        return false;
    };
};

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.