From 7095a49ff3dfe2d9b004b4c4b3590f6f257221d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?=
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the For example, Concerning boolean attributes, consider a DOM element defined by the HTML markup Concerning boolean attributes, consider a DOM element defined by the HTML markup When setting multiple attributes, the quotes around attribute names are optional. To remove an attribute, either call Note: Because ARIA attributes frequently associate behavior with "false" values that differs from attribute absence, passing WARNING: When setting the 'class' attribute, you must always use quotes! Note: Attempting to change the Attributes vs. Properties
.attr()
method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop()
method provides a way to explicitly retrieve property values, while .attr()
retrieves attributes.selectedIndex
, tagName
, nodeName
, nodeType
, ownerDocument
, defaultChecked
, and defaultSelected
should be retrieved and set with the .prop()
method. Prior to jQuery 1.6, these properties were retrievable with the .attr()
method, but this was not within the scope of attr
. These do not have corresponding attributes and are only properties.<input type="checkbox" checked="checked" />
, and assume it is in a JavaScript variable named elem
:<input type="checkbox" checked="" />
, and assume it is in a JavaScript variable named elem
:
+
@@ -44,7 +44,14 @@
- elem.getAttribute( "checked" )
+ "checked"
(String) Initial state of the checkbox; does not change
+ ""
(String) Initial state of the checkbox; does not change
+
+
+ $( elem ).attr( "checked" )
+ (4.0+)
+ ""
(String) Initial state of the checkbox; does not change
@@ -131,6 +138,7 @@ The title of the emphasis is:
null
, the specified attribute will be removed (as in .removeAttr()
).null
, the specified attribute will be removed (as in .removeAttr()
). Non-ARIA attributes can also be removed by passing false
.Removing an attribute
+ .attr( name, null )
or use .removeAttr( name )
. For non-ARIA attributes, in jQuery 4.0+ you can also call .attr( name, false )
.false
as the value for an attribute whose name starts with "aria-…"
will stringify that value to "false"
rather than remove the attribute. To guarantee removal of an attribute, provide null
as the value or use the .removeAttr()
method.type
attribute on an input
or button
element created via document.createElement()
will throw an exception on Internet Explorer 8 or older.