Browse - Programming Tips - Using jQuery, how can I get the value of a checkbox?

Date: 2016feb3
Library: jQuery
Language: javaScript

Q.  Using jQuery, how can I get the value of a checkbox?

A.  Assuming you have:

<form> <input name=mycheck> My Check </form>
Get the value as a boolean:
var mycheck = $('input[name=mycheck]')[0].checked;
This uses [0] to convert the jQuery object into regular javaScript then uses .checked. So it won't act nicely if the object doesn't exist. Works with all versions of jQuery. Get the value as "yes" or "no":
var mycheck = toYesNo($('input[name=mycheck]')[0].checked);
Using this function:
function toYesNo(b) { return b ? 'yes' : 'no'; }
Do NOT use val():
var mycheck = $('input[name=mycheck]').val(); // WRONG // Gives you value= from the html no matter if checked or not