Reputation: 12621
I am new to jquery. I have below the code to find out whether the check box is checked or not. But though the check box is checked it always goes to else block in below the code:
if ($('#someId').is(':checked')) {
alert("checked");
} else {
alert("unchecked");
}
Html code:
By default the check box is not checked. The code is as below.
<td>
select :<br><input type="checkbox" id="someId" name="someId" value="">
</td>
Am I doing anything wrong here? Thanks!
Upvotes: 0
Views: 900
Reputation: 2725
try this:
select :<input type="checkbox" id="someId" name="someId" value="" checked />
<input id="btnSubmit" type="button" value="Submit"/>
$("#btnSubmit").on("click",function(){
if ($("#someId").is(':checked')) {
alert("checked");
} else {
alert("unchecked");
}
});
working fiddle here: http://jsfiddle.net/tLebs/1/
i hope it helps.
Upvotes: 1
Reputation: 2025
I am throwing out a guess that you need a doc ready function
$( document ).ready(function() {
$("#someId").on("click",function(){
if ($(this).is(':checked')) {
alert("checked");
} else {
alert("unchecked");
}
});
});`
see http://learn.jquery.com/using-jquery-core/document-ready/
Upvotes: 0
Reputation: 324487
Skip jQuery's methods for this if you're only dealing with one checkbox: the DOM checked
property is as easy as it could possibly be. jQuery only confuses the issue. Feel free to use jQuery to get hold of the element though.
var isChecked = $("#someId")[0].checked;
Upvotes: 1