Reputation: 6820
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
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
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
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
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
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
Reputation: 220136
if( $('#terminalverified').is(':checked') ) {
alert("yes");
} else {
alert("no");
}
You can also use this:
if( $('#terminalverified')[0].checked ) {
Upvotes: 3