![]() The same is true for other dynamic attributes, such as selected and value. Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property: The checked attribute value does not change with the state of the checkbox, while the checked property does. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property. True (Boolean) Changed with checkbox stateĪccording 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". "checked" (String) Initial state of the checkbox does not change True (Boolean) Will change with checkbox state These do not have corresponding attributes and are only properties.Ĭoncerning boolean attributes, consider a DOM element defined by the HTML markup, and assume it is in a JavaScript variable named elem: elem.checked For any non-empty XML document, documentElement will always be whatever element is the root element of the document. attr() method, but this was not within the scope of attr. For any non-empty HTML document, documentElement will always be an element.Prior to jQuery 1.6, these properties were retrievable with the. attr() retrieves attributes.įor example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the. ![]() prop() method provides a way to explicitly retrieve property values, while. attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. The difference between attributes and properties can be important in specific situations. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the. attr() method returns undefined for attributes that have not been set. Note: Attribute values are strings with the exception of a few attributes such as value and tabindex.Īs of jQuery 1.6, the.
0 Comments
Leave a Reply. |