Checkboxes are an important part of forms, and knowing whether they are checked, in JavaScirpt and jQuery, can be very useful.
Standard checking in JavaScript is very simple...
console.log("Checked status?" + document.getElementById('MyCheckBox').checked);
Checking in jQuery is similarly not very difficult...
console.log("Checked status?" + $('#MyCheckBox').is(":checked"));
If you have a newer version of jQuery (v. 1.6+), the following will also work...
console.log("Checked status?" + $('#MyCheckBox').prop('checked'));
Make sure not to mix these up, or you will get bad results. jQuery selectors return an array of matched elements. If you try to look at $('#MyCheckBox').checked, you will always see false, whereas $('#MyCheckBox')[0].checked would be more likely to work.
No comments:
Post a Comment