RussellHarrower
RussellHarrower

Reputation: 6820

jQuery check box

Just wondering how I can tell if a checkbox is checked or not I tried this

if($('#terminalverified').is(':checked')){"yes"}else{"no"});

however this did not work, infact what it did was stop my script working all together

Upvotes: 0

Views: 860

Answers (6)

Dipak
Dipak

Reputation: 125

Javascript:

<script>
    var group = document.myForm.check;
    for (var i = 0; i < group.length; i++) {
        if (group[i].checked)
            break;
    }
    if (i == group.length) {
        alert("Please check some value");
        return false;
    }
</script>

HTML:

 <form name="myForm" onsubmit="return validateForm();" method="post">
        <input type="checkbox" name="checksector" id="check">
        <input type="checkbox" name="checksector" id="check">
        <input type="checkbox" name="checksector" id="check">
        <input type="checkbox" name="checksector" id="check">
        <input value="Save" type="submit">Register</button>
    </form>

you can refer http://www.chennaisunday.com/jscheckbox.html

Upvotes: 0

Paul
Paul

Reputation: 141917

You're using jQuery correctly, but your Javascript syntax is incorrect . Try this:

if($('#terminalverified').is(':checked')){
    // Yes it is checked
}else{
    // No it is not checked
};

I removed the ) at the end of your if statement between the } and ; that was causing the error.

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69915

There is an extra closing bracket()) in your code otherwise your code is perfect to check whether checkbox is checked or not.

if($('#terminalverified').is(':checked')){alert("yes")}else{alert("no");};

Upvotes: 1

Chris Fulstow
Chris Fulstow

Reputation: 41902

Try this:

<input type="checkbox" id="terminalverified" /> Terminal verified

<script>
$(function() {
    $("#terminalverified").click(function() {
        if( $(this).is(':checked') ) {
            alert("yes");
        } else {
            alert("no");
        };
    });
});
</script>

Upvotes: 1

Jess
Jess

Reputation: 8700

Idk what you think the strings inside the if statement are going todo.

You could try

if($('#terminalverified').is(':checked'))
{
    alert("yes");
}
else
{
    alert("no");
}

or

alert(($('#terminalverified').is(':checked'))?'yes':'no');

Upvotes: 3

Joseph Silber
Joseph Silber

Reputation: 220136

if( $('#terminalverified').is(':checked') ) {
    alert("yes");
} else {
    alert("no");
}

You can also use this:

if( $('#terminalverified')[0].checked ) {

Upvotes: 3

Related Questions