0

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!

5
  • what's wrong?: jsfiddle.net/fEThD Commented Aug 13, 2013 at 13:56
  • have you put your javascript code into DOM-ready event, i.e. $(function() { /* your code here */ });
    – Dennis
    Commented Aug 13, 2013 at 13:56
  • What event causes the code to run? Commented Aug 13, 2013 at 13:57
  • On check box selection i am not doing anything. have one submit button. On submit, am checking whether the checkbox is checked or not...on check box selection should i set the value to checked? Commented Aug 13, 2013 at 14:00
  • Is this functionality wrapped in $().ready()? Commented Aug 13, 2013 at 14:02

4 Answers 4

1

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.

1
  • Thanks for ur reply. On check box selection i am not doing anything. have one submit button. On submit, am checking whether the checkbox is checked or not...on check box selection should i set the value to checked? Commented Aug 13, 2013 at 14:00
1

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;
0

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/

0
 if ($("#Your_CheckBox").is(':checked')) {

    }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.