In JavaScript, if we write the following for example:
var c = this.checked;
What is checked
here? Is it just a state
Assuming this
refers to a DOM element which has a checked
property (e.g. a checkbox or a radio button) then the checked
property will either be true
if the element is checked, or false
if it's not. For example, given this HTML:
<input type="checkbox" id="example">
The following line of JS will return false
:
var c = document.getElementById("example").checked; //False
Note that what you've written is standard JavaScript, not jQuery. If this
refers to a jQuery object rather than a DOM element, checked
will be undefined because the jQuery object does not have a checked
property. If this
is a jQuery object, you can use .prop:
var c = this.prop("checked");
To use the pseudo selector :checked
with the jquery object this
write:
$(this).is(':checked')
In jQuery checked is a selector:
The :checked selector works for checkboxes and radio buttons.
There are some ways to check if a checkbox is checked or not:
For Example:
$('#checkBox').attr('checked');
or
$('#checkBox').is(':checked');
Is it just a state that tells us if a checkbox for example is checked or not? So, can we use it to check that the checkbox is also not checked?
Yes and yes