JavaScript this.checked

In JavaScript, if we write the following, for example:

var c = this.checked; 

What is checked here? Is this just a state that tells us if the checkbox is checked or not? So, can we use it to verify that the checkbox is also not checked?

+6
source share
4 answers

Is it just a state that tells us if the checkbox is checked or not? So, can we use it to verify that the checkbox is also not checked?

Yes and yes

+8
source

To use the :checked pseudo- :checked with a jquery this object, write:

 $(this).is(':checked') 
+36
source

Assuming this refers to a DOM element that has a checked property (for example, a check box or radio button), then the checked property will be either true if the element is checked, or false if not. For example, given this HTML:

 <input type="checkbox" id="example"> 

The following JS line will return false :

 var c = document.getElementById("example").checked; //False 

Note that you wrote standard JavaScript, not jQuery. If this refers to a jQuery object and not a DOM element, checked will be undefined because the jQuery object does not have the checked property. If this is a jQuery object, you can use .prop :

 var c = this.prop("checked"); 
+8
source

In jQuery validation, a selector is selected:

The: checked selector works for checkboxes and radio buttons.

There are several ways to check if the checkbox is checked:

Example:

 $('#checkBox').attr('checked'); or $('#checkBox').is(':checked'); 
+4
source

Source: https://habr.com/ru/post/898740/


All Articles