Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 27, 2023 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/00b2785a2c189816bca70d011040b0c2 to your computer and use it in GitHub Desktop.
Save code-boxx/00b2785a2c189816bca70d011040b0c2 to your computer and use it in GitHub Desktop.
Javascript Get & Set HTML Data Attribute

JAVASCRIPT GET & SET HTML DATA

https://code-boxx.com/set-get-html-data-attributes/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Set Data Attribute</title>
</head>
<body>
<!-- (A) SET DATA ATTRIBUTE IN HTML START TAG -->
<div id="demoA" data-color="red">
This element has custom data attributes.
</div>
<!-- (B) SET DATA ATTRIBUTE IN JAVASCRIPT -->
<script>
document.getElementById("demoA").dataset.priority = "high";
// <div id="demoA" data-color="red" data-priority="high">
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Get Data Attribute</title>
</head>
<body>
<!-- (A) ELEMENT WITH DATA ATTRIBUTES -->
<div id="demoB" data-color="red" data-priority="high">
This element has custom data attributes.
</div>
<!-- (B) GET DATA ATTRIBUTE IN JAVASCRIPT -->
<script>
var el = document.getElementById("demoB");
console.log(el.dataset.color); // red
console.log(el.dataset.priority); // high
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Set Array Into Data Attribute</title>
</head>
<body>
<!-- (A) DUMMY ELEMENT -->
<div id="demoC">Dummy</div>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) GET HTML ELEMENT
var el = document.getElementById("demoC");
// (B2) SET ARRAY TO DATA ATTRIBUTE
// dataset can only be string - json.stringify() to turn array to string
el.dataset.array = JSON.stringify(["Red", "Green", "Blue"]);
// (B3) GET ARRAY BACK FROM STRING
// use json.parse() to turn encoded string back to array
console.log(JSON.parse(el.dataset.array));
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Set HTML Snippet In Data Attribute</title>
</head>
<body>
<!-- (A) DUMMY ELEMENTS -->
<div id="demoD" data-snippet="<strong>Foo</strong>">Demo D</div>
<div id="demoE">Demo E</div>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) GET HTML SNIPPET
var snippet = document.getElementById("demoD").dataset.snippet;
// (B2) SET HTML SNIPPET
document.getElementById("demoE").innerHTML = snippet;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Set CSS Style Based On Data Attribute</title>
<!-- (A) CSS STYLES -->
<style>
div[data-color="red"] { background: red; }
div[data-color="green"] { background: green; }
div[data-color="blue"] { background: blue; }
</style>
</head>
<body>
<!-- (B) DUMMY ELEMENT -->
<div id="demoF">Dummy</div>
<!-- (C) JAVASCRIPT SET COLOR -->
<script>
function setColor (col) {
document.getElementById("demoF").dataset.color = col;
}
</script>
<input type="button" value="Red" onclick="setColor('red')">
<input type="button" value="Green" onclick="setColor('green')">
<input type="button" value="Blue" onclick="setColor('blue')">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment