Source Code:
(back to article)
Submit
Result:
Report an issue
<div id="demo" class="sample" data-level="1">Hello, World!</div> <br /> <div>first getAttributes result (data-level): <span id="1"></span></div> <div>className property: <span id="2"></span></div> <script> const demoElement = document.getElementById("demo"); const span1 = document.getElementById("1"); const span2 = document.getElementById("2"); // Accessing an attribute let classAttribute = demoElement.getAttribute("data-level"); // Output: "1" span1.innerHTML = classAttribute; // Accessing a property const classNameProperty = demoElement.className; // Output: "sample" span2.innerHTML = classNameProperty; </script>