Skip to content

Commit 5f0da17

Browse files
committed
Attr: Document jQuery 4 boolean attribute changes; add a 4.0 version category
Fixes gh-1243 Ref jquery/jquery#5452 Ref jquery/jquery-migrate#540
1 parent b026c03 commit 5f0da17

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

categories.xml

+11
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,17 @@ var files = event.originalEvent.dataTransfer.files;
519519
<hr/>
520520
]]></desc>
521521
</category>
522+
<category name="Version 4.0" slug="4.0">
523+
<desc><![CDATA[
524+
<div id="version-support-warning" class="warning"><i class="icon-info-sign"></i> <span>This is a pre-release. Behavior may change before the stable release is cut. Use with caution.</span></div>
525+
<p>Aspects of the API that were changed in the corresponding version of jQuery.</p>
526+
<p>Dropped support for IE &lt;11 & Edge Legacy, removed deprecated APIs, added FormData support, improved support for CSP & Trusted Types. Automatic JSONP promotion removed. Special handling of boolean attributes removed.</p>
527+
<p>Callbacks & Deferred modules are now excluded from the Slim build.</p>
528+
<p>jQuery is now authored in ESM.</p>
529+
<p>For more information, see the <a href="https://blog.jquery.com/2024/07/17/second-beta-of-jquery-4-0-0/">Release Notes/Changelog of jQuery 4.0.0-beta.2</a>.</p>
530+
<hr/>
531+
]]></desc>
532+
</category>
522533
<category name="All" slug="all"/>
523534
</category>
524535
</categories>

entries/attr.xml

+19-4
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,7 +44,14 @@
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>
@@ -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)