You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: entries/attr.xml
+20-5
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@
26
26
<h4>Attributes vs. Properties</h4>
27
27
<p>The difference between <em>attributes</em> and <em>properties</em> can be important in specific situations. <strong>Before jQuery 1.6</strong>, the <code>.attr()</code> method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. <strong>As of jQuery 1.6</strong>, the <code>.prop()</code> method provides a way to explicitly retrieve property values, while <code>.attr()</code> retrieves attributes.</p>
28
28
<p>For example, <code>selectedIndex</code>, <code>tagName</code>, <code>nodeName</code>, <code>nodeType</code>, <code>ownerDocument</code>, <code>defaultChecked</code>, and <code>defaultSelected</code> should be retrieved and set with the <code><ahref="/prop/">.prop()</a></code> method. Prior to jQuery 1.6, these properties were retrievable with the <code>.attr()</code> method, but this was not within the scope of <code>attr</code>. These do not have corresponding attributes and are only properties.</p>
29
-
<p>Concerning boolean attributes, consider a DOM element defined by the HTML markup <code><input type="checkbox" checked="checked" /></code>, and assume it is in a JavaScript variable named <code>elem</code>:</p>
29
+
<p>Concerning boolean attributes, consider a DOM element defined by the HTML markup <code><input type="checkbox" checked="" /></code>, and assume it is in a JavaScript variable named <code>elem</code>:</p>
30
30
<table>
31
31
<tr>
32
32
<th>
@@ -44,12 +44,19 @@
44
44
<th>
45
45
<code>elem.getAttribute( "checked" )</code>
46
46
</th>
47
-
<td><code>"checked"</code> (String) Initial state of the checkbox; does not change</td>
47
+
<td><code>""</code> (String) Initial state of the checkbox; does not change</td>
48
+
</tr>
49
+
<tr>
50
+
<th>
51
+
<code>$( elem ).attr( "checked" )</code>
52
+
<em>(4.0+)</em>
53
+
</th>
54
+
<td><code>""</code> (String) Initial state of the checkbox; does not change</td>
48
55
</tr>
49
56
<tr>
50
57
<th>
51
58
<code>$( elem ).attr( "checked" )</code>
52
-
<em>(1.6+)</em>
59
+
<em>(1.6-3.x)</em>
53
60
</th>
54
61
<td><code>"checked"</code> (String) Initial state of the checkbox; does not change</td>
55
62
</tr>
@@ -131,6 +138,7 @@ The title of the emphasis is:<div></div>
131
138
<categoryslug="version/1.0"/>
132
139
<categoryslug="version/1.1"/>
133
140
<categoryslug="version/1.6"/>
141
+
<categoryslug="version/4.0"/>
134
142
</entry>
135
143
<entrytype="method"name="attr"return="jQuery">
136
144
<signature>
@@ -142,7 +150,8 @@ The title of the emphasis is:<div></div>
142
150
<typename="String"/>
143
151
<typename="Number"/>
144
152
<typename="Null"/>
145
-
<desc>A value to set for the attribute. If <code>null</code>, the specified attribute will be removed (as in <ahref="/removeAttr/"><code>.removeAttr()</code></a>).</desc>
153
+
<typename="Boolean"/>
154
+
<desc>A value to set for the attribute. If <code>null</code>, the specified attribute will be removed (as in <ahref="/removeAttr/"><code>.removeAttr()</code></a>). Non-ARIA attributes can also be removed by passing <code>false</code>.</desc>
146
155
</argument>
147
156
</signature>
148
157
<signature>
@@ -162,7 +171,7 @@ The title of the emphasis is:<div></div>
162
171
<argumentname="attr"type="String" />
163
172
<return>
164
173
<typename="String"/>
165
-
<typename="Number"/>
174
+
<typename="Number"/>
166
175
</return>
167
176
</argument>
168
177
</signature>
@@ -188,6 +197,11 @@ $( "#greatphoto" ).attr({
188
197
});
189
198
</code></pre>
190
199
<p>When setting multiple attributes, the quotes around attribute names are optional.</p>
200
+
<h4id="removing-attr">Removing an attribute</h4>
201
+
<p>To remove an attribute, either call <code>.attr( name, null )</code> or use <ahref="/removeAttr/"><code>.removeAttr( name )</code></a>. For non-ARIA attributes, in jQuery 4.0+ you can also call <code>.attr( name, false )</code>.</p>
202
+
<divclass="warning">
203
+
<p><strong>Note:</strong> Because <ahref="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA">ARIA</a> attributes frequently associate behavior with "false" values that differs from attribute absence, passing <code>false</code> as the value for an attribute whose name starts with <code>"aria-…"</code> will stringify that value to <code>"false"</code> rather than remove the attribute. To guarantee removal of an attribute, provide <code>null</code> as the value or use the <code>.removeAttr()</code> method.</p>
204
+
</div>
191
205
<p><strong>WARNING</strong>: When setting the 'class' attribute, you must always use quotes!</p>
192
206
<divclass="warning">
193
207
<p><strong>Note:</strong> Attempting to change the <code>type</code> attribute on an <code>input</code> or <code>button</code> element created via <code>document.createElement()</code> will throw an exception on Internet Explorer 8 or older.</p>
0 commit comments