From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/html/global_attributes%2Fdata-%2A.html | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 devdocs/html/global_attributes%2Fdata-%2A.html (limited to 'devdocs/html/global_attributes%2Fdata-%2A.html') diff --git a/devdocs/html/global_attributes%2Fdata-%2A.html b/devdocs/html/global_attributes%2Fdata-%2A.html new file mode 100644 index 00000000..33aee82d --- /dev/null +++ b/devdocs/html/global_attributes%2Fdata-%2A.html @@ -0,0 +1,74 @@ +

data-*

The data-* global attributes form a class of attributes called custom data attributes, that allow proprietary information to be exchanged between the HTML and its DOM representation by scripts.

+

Try it

+
+

All such custom data are available via the HTMLElement interface of the element the attribute is set on. The HTMLElement.dataset property gives access to them. The * may be replaced by any name following the production rule of XML names which includes the following recommendations:

These are recommendations. If these naming recommendations are not followed, no errors will occur. The attributes will still be matched using CSS attribute selectors, with the attribute being case insensitive and any attribute value being case-sensitive. Attributes not conforming to these three recommendations will also still be recognized by the JavaScript HTMLElement.dataset property and user-agents will include the attribute in the DOMStringMap containing all the custom data attributes for an HTMLElement.

If you plan to use HTMLElement.dataset, the portion of the attribute name following the data- can only include characters allowed in JavaScript property names (and hyphens, which will be removed). The dataset version of the attribute name removes the "data-" prefix and converts the rest of the name from kebab-case to camelCase. For example, element.getAttribute("data-test") is equivalent to element.dataset.test and data-test-abc will be accessible as HTMLElement.dataset.testAbc (or by HTMLElement.dataset["testAbc"]). Avoid non-alphabetic characters following a hyphen, such as data-test-1 or data--test, as they will not be recognized by HTMLElement.dataset.

+
+

Usage

+
+

By adding data-* attributes, even ordinary HTML elements can become rather complex and powerful program-objects. For example, a space-ship "sprite" in a game could be a simple <img> element with a class attribute and several data-* attributes:

+

html

+
<img
+  class="spaceship cruiserX3"
+  src="shipX3.png"
+  data-ship-id="324"
+  data-weapons="laserI laserII"
+  data-shields="72%"
+  data-x="414354"
+  data-y="85160"
+  data-z="31940"
+  onclick="spaceships[this.dataset.shipId].blasted()" />
+
+

For a more in-depth tutorial about using HTML data attributes, see Using data attributes.

+
+

Specifications

+
+ + +
Specification
HTML Standard
# attr-data-*
+

Browser compatibility

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariWebView AndroidChrome AndroidFirefox for AndroidOpera AndroidSafari on IOSSamsung Internet
data-*7126Yes155.14.41861451.0
+

See also

+
+

+ © 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
+ https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-* +

+
-- cgit v1.2.3