-2

Possible Duplicate:
How do I check if a checkbox is checked with JQuery?

I have a following form which opens in light box

<form name="adminFormMod" id="adminFormMod" action="index.php" method="post"> <input type="text" name="myname" value="sss"/> <input type="checkbox" name="test" id="test" value="tttt" /> <input type="button" value="submit" onclick="updateresults()"/> </form> 

On the js page which includes on the above page when i try to check weather the checkbox is checked or not.my following code is not working

alert(document.getElementById('test').checked); 

the above always alert the fasle

Even with following jquery line of code

$('#adminFormMod').find(':input').each(function() { // alert('idhar'); switch(this.type) { case 'text': //alert('text'); $(this).val(''); break; case 'checkbox': //alert($(this).attr('checked')); if($(this).attr('name')=='test') { alert(this.checked); alert($(this).attr('checked')); } break; } } ); 

it always alert the false even the checkbox is checked.Please help

1
  • before ask a question , at least do a Google search............ don't put any thing with out a research Commented Nov 7, 2011 at 7:22

4 Answers 4

2

Try this:

$('#yourCheckbox').is(':checked') 
Sign up to request clarification or add additional context in comments.

3 Comments

can i use keyword (this) instead of #yourcheckbox because there is gone a be multiple checkbox which will create dynamicaly
It depends what "this" is in your context. If you iterate over a collection with some jQuery selector, then this is in most cases the current jQuery element captured by that selector; hence you can do something like this.is(":checked") or alternatively if it is the DOM element $(this).is(":checked"). But in your case you might also want to try capturing all checked inputs with some more generic selector like $("input:checked").each(function...
thank you for your answers and comments but i think there is some problem because as i said my form is opening in lightbox so the code is not giving the right result.what i should i do then?please help
2

probably a duplicate of: How do I check if a checkbox is checked with JQuery?

checked being a property and not an attribute you need to write the code slightly differently:

$('#checkbox').is(":checked") 

Comments

0
<script> var n = $("input:checked").length; if(n>0) { alert('checked'); } } </script> 

http://oops-solution.blogspot.com/

Comments

0

some others ways are

// First way $('#checkBox').attr('checked'); // Second way $('#edit-checkbox-id').is(':checked'); // Third way for jQuery 1.2 $("input[@type=checkbox][@checked]").each( function() { // Insert code here } ); 

http://oops-solution.blogspot.com/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.