From 7095a49ff3dfe2d9b004b4c4b3590f6f257221d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?= Date: Wed, 9 Oct 2024 18:13:07 +0200 Subject: [PATCH] Attr: Document jQuery 4 boolean attribute changes Fixes gh-1243 Ref jquery/jquery#5452 Ref jquery/jquery-migrate#540 --- entries/attr.xml | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/entries/attr.xml b/entries/attr.xml index 03baf91c..d74cf22c 100644 --- a/entries/attr.xml +++ b/entries/attr.xml @@ -26,7 +26,7 @@

Attributes vs. Properties

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .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.

For example, 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.

-

Concerning boolean attributes, consider a DOM element defined by the HTML markup <input type="checkbox" checked="checked" />, and assume it is in a JavaScript variable named elem:

+

Concerning boolean attributes, consider a DOM element defined by the HTML markup <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:
+ @@ -142,7 +150,8 @@ The title of the emphasis is:
- A value to set for the attribute. If null, the specified attribute will be removed (as in .removeAttr()). + + A value to set for the attribute. If null, the specified attribute will be removed (as in .removeAttr()). Non-ARIA attributes can also be removed by passing false.
@@ -162,7 +171,7 @@ The title of the emphasis is:
- +
@@ -188,6 +197,11 @@ $( "#greatphoto" ).attr({ });

When setting multiple attributes, the quotes around attribute names are optional.

+

Removing an attribute

+

To remove an attribute, either call .attr( name, null ) or use .removeAttr( name ). For non-ARIA attributes, in jQuery 4.0+ you can also call .attr( name, false ).

+
+

Note: Because ARIA attributes frequently associate behavior with "false" values that differs from attribute absence, passing 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.

+

WARNING: When setting the 'class' attribute, you must always use quotes!

Note: Attempting to change the type attribute on an input or button element created via document.createElement() will throw an exception on Internet Explorer 8 or older.

@@ -273,5 +287,6 @@ $( "img" ).attr( "src", function() { +