Testing if a checkbox is checked with jQuery
Solution 1:
Use .is(':checked')
to determine whether or not it's checked, and then set your value accordingly.
More information here.
Solution 2:
$("#ans").attr('checked')
will tell you if it's checked. You can also use a second parameter true/false to check/uncheck the checkbox.
$("#ans").attr('checked', true);
Per comment, use prop
instead of attr
when available. E.g:
$("#ans").prop('checked')
Solution 3:
Just use $(selector).is(':checked')
It returns a boolean value.
Solution 4:
// use ternary operators
$("#ans").is(':checked') ? 1 : 0;