Skip to content

Commit a766bc4

Browse files
committed
Attr: Document jQuery 4 boolean attribute changes
Fixes gh-1243 Ref jquery/jquery#5452 Ref jquery/jquery-migrate#540
1 parent eacda2b commit a766bc4

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

entries/attr.xml

+20-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<h4>Attributes vs. Properties</h4>
2727
<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>
2828
<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><a href="/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>&lt;input type="checkbox" checked="checked" /&gt;</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>&lt;input type="checkbox" checked="" /&gt;</code>, and assume it is in a JavaScript variable named <code>elem</code>:</p>
3030
<table>
3131
<tr>
3232
<th>
@@ -44,12 +44,19 @@
4444
<th>
4545
<code>elem.getAttribute( "checked" )</code>
4646
</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>
4855
</tr>
4956
<tr>
5057
<th>
5158
<code>$( elem ).attr( "checked" )</code>
52-
<em>(1.6+)</em>
59+
<em>(1.6-3.x)</em>
5360
</th>
5461
<td><code>"checked"</code> (String) Initial state of the checkbox; does not change</td>
5562
</tr>
@@ -131,6 +138,7 @@ The title of the emphasis is:<div></div>
131138
<category slug="version/1.0"/>
132139
<category slug="version/1.1"/>
133140
<category slug="version/1.6"/>
141+
<category slug="version/4.0"/>
134142
</entry>
135143
<entry type="method" name="attr" return="jQuery">
136144
<signature>
@@ -142,7 +150,8 @@ The title of the emphasis is:<div></div>
142150
<type name="String"/>
143151
<type name="Number"/>
144152
<type name="Null"/>
145-
<desc>A value to set for the attribute. If <code>null</code>, the specified attribute will be removed (as in <a href="/removeAttr/"><code>.removeAttr()</code></a>).</desc>
153+
<type name="Boolean"/>
154+
<desc>A value to set for the attribute. If <code>null</code>, the specified attribute will be removed (as in <a href="/removeAttr/"><code>.removeAttr()</code></a>). Non-ARIA attributes can also be removed by passing <code>false</code>.</desc>
146155
</argument>
147156
</signature>
148157
<signature>
@@ -162,7 +171,7 @@ The title of the emphasis is:<div></div>
162171
<argument name="attr" type="String" />
163172
<return>
164173
<type name="String"/>
165-
<type name="Number"/>
174+
<type name="Number"/>
166175
</return>
167176
</argument>
168177
</signature>
@@ -188,6 +197,11 @@ $( "#greatphoto" ).attr({
188197
});
189198
</code></pre>
190199
<p>When setting multiple attributes, the quotes around attribute names are optional.</p>
200+
<h4 id="removing-attr">Removing an attribute</h4>
201+
<p>To remove an attribute, either call <code>.attr( name, null )</code> or use <a href="/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+
<div class="warning">
203+
<p><strong>Note:</strong> Because <a href="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>
191205
<p><strong>WARNING</strong>: When setting the 'class' attribute, you must always use quotes!</p>
192206
<div class="warning">
193207
<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>
@@ -273,5 +287,6 @@ $( "img" ).attr( "src", function() {
273287
<category slug="version/1.0"/>
274288
<category slug="version/1.1"/>
275289
<category slug="version/1.6"/>
290+
<category slug="version/4.0"/>
276291
</entry>
277292
</entries>

0 commit comments

Comments
 (0)