Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which way to test if an element is checked is better? .is(':checked') or .prop('checked')

Tags:

jquery

checked

Both .is(':checked') and .prop('checked') can be used to test if a checkbox is checked.

Are there any interesting/important differences between those two ways of querying the checked state or is it pretty much only a matter of personal preference?

like image 503
ThiefMaster Avatar asked Jun 14 '11 07:06

ThiefMaster


People also ask

How do you check if an element is checked or not?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

Which property indicate if the checkbox is checked or not?

$('#element').is(':checked')) The :checked selector was specifically made for checking whether radio button or checkbox element have been selected or not.

Which of the following is the correct code for determining if a checkbox is checked?

We can check the status of a checkbox by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') .

How do you check whether the checkbox is checked or not in angular?

The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.


2 Answers

They both end up checking the same thing.

If you're using 1.6.0 or higher, prop('checked') is the most direct jQuery way. jQuery doesn't have to parse and process a selector to figure out what to do.[Note below]

You can also (as of 1.6.1) use attr('checked') again as with 1.5.x and earlier.

Or you can go directly to the element. Assuming x is the thing to be tested, if you know that at least one element matched, then:

if (x[0].checked) { /* ... */ }

If you're not sure and want to hedge your bets:

if (x[0] && x[0].checked) { /* ... */ }

But unless you're in a really tight loop, use whatever you find easiest to read, as the performance differences aren't going to matter. I find the last ones quite easy to read and I know they're very fast, so I use them. But if you find them awkward, use whatever you like best. No harm in using is(':checked') if you like it and you're not seeing an actual performance hit from it (which is unlikely barring, again, some kind of tight loop).


Note: The degree to which prop is more direct than is varies by browser. prop isn't just a direct check of a property, it does go through a couple of levels of indirection first; and is isn't necessarily hugely complex: On WebKit browsers, for instance, is can be fairly direct as WebKit provides a function to test whether an element matches a selector, and supports :checked natively; on Firefox (and, I suspect, IE), is results in a huge number of function calls as this seemingly-simple selector works its way through the guts of Sizzle.

like image 58
T.J. Crowder Avatar answered Sep 30 '22 02:09

T.J. Crowder


I would use prop('checked') myself (so long as I didn't have to support older jQuery versions) as it is accessing the checked property directly of the object and is easy enough to read.

is(':checked') runs a bit of extra overhead with string parsing, etc. I generally reserve :checked for when selecting elements.

like image 26
alex Avatar answered Sep 30 '22 02:09

alex