21

For the above checkbox

document.getElementById("checkbox1").checked // -> returns true

But

var byAppr = document.getElementById('checkbox1').value;
$(byAppr).attr('checked') // -> returns undefined

I am testing this in firefox 3.6

2
  • which version of jquery are you using? Commented Jun 29, 2011 at 22:48
  • var byAppr = document.getElementById('checkboxName').value; $(byAppr).attr('checked') // -> returns undefined Commented Jun 29, 2011 at 23:31

2 Answers 2

76

Use one of the following:

  • $('#checkbox1').prop('checked') - in jQuery 1.6+, usually the way to go
  • $('#checkbox1').is(':checked') - all jQuery versions, but slower
  • $('#checkbox1').attr('checked') - NOT in jQuery 1.6 - but in 1.6.1 and <=1.5, don't use it

Also, in cases where you already have the DOM element available directly (e.g. this in an event handler bound to the field), use this.checked instead of $(this) with one of the methods above!

Sign up to request clarification or add additional context in comments.

Comments

2

You're selecting based on value. I think you want:

var byAppr = document.getElementById('checkbox1');
$(byAppr).attr('checked')

2 Comments

Actuall this is exactly what i am doing ...that is resulting in undefined return value var byAppr = document.getElementById('checkboxName').value; byAppr = '#'+ byAppr; $(byAppr).attr('checked');
You're still including .value at the end of the first expression. If you exclude that it should work as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.