0

I am using following code to detect whether the check box inside my gridview template field is checked or not. If none of the check box is selected then I want to show alert message.

   function findCheckBox() {

        var inputElements = document.getElementsByTagName('input');
        var chekSelect = false;
        for (var i = 0; i < inputElements.length; i++) {
            var myElement = inputElements[i];


            if (myElement.type === "checkbox") {

                if (myElement.checked === false) {

                    chekSelect = true;
                    return true;
                }
            }

            if (chekSelect === true) {
                return true;
            }

            else {
                alert('Please Check Atleast one record to print cheque!!!');
                return false;
            }
        }  

    }

But with this code when I click on my button its showing me error message for one time even if one or more check box is checked. What I am doing wrong here. Can anyone help me please.

1
  • I am so sorry but I dont know Jquery..can you please tell me how can I use it? Commented Jul 4, 2013 at 8:08

3 Answers 3

3

Your logic is slightly off. Corrected version:

jsFiddle demo

function findCheckBox() {
    var inputElements = document.getElementsByTagName('input');
    var chekSelect = false;
    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];

        if (myElement.type === "checkbox") {
            if (myElement.checked) {
                chekSelect = true;
                break;
            }
        }
    }

    if(!chekSelect) {
        alert('Please Check Atleast one record to print cheque!!!');
        return false;
    } else {
        return true;
    }
}

I've changed the .checked test, to test for it being true not false, because you want to know if at least one checkbox is checked. I also added a break, and moved the alert to outside of the for, because you won't know if there is a checkbox checked until the for completes.

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

Comments

3

Try this

function findCheckBox() {
    var inputElements = document.getElementsByTagName('input');
    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];
        if (myElement.type === "checkbox" && myElement.checked) {
            return true;
        }
    }
    alert('Please Check Atleast one record to print cheque!!!');
    return false;
}

Comments

1

Using JQuery:

var checked = false;
$('input:checkbox').each(function(){
    if($(this).prop('checked')){
        checked = true;
        break;
    }
});

if(!checked) alert('Please Check At least one record to print cheque!!!')

1 Comment

This could be simplified down to if(!$('input[type=checkbox]:checked').length) alert('...');.

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.