Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elem.is(':checked') vs elem.prop('checked') [duplicate]

Tags:

jquery

As title says, whats the difference between elem.is(':checked') and elem.prop('checked').

I personally use elem.is(':checked') because function is explains that I am going to return boolean.

When shall I choose one over the other and why?

like image 561
Imad Avatar asked Apr 18 '17 05:04

Imad


People also ask

What is checked in prop?

According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or is set to empty string value or even "false". This is true of all boolean attributes.

What is prop in jQuery?

jQuery prop() Method The prop() method sets or returns properties and values of the selected elements. When this method is used to return the property value, it returns the value of the FIRST matched element.


1 Answers

Answer based on This SO Question.

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: 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.

Test Result : jQuery prop('checked') vs. is(':checked')

like image 126
4b0 Avatar answered Oct 27 '22 00:10

4b0