Skip to content

Instantly share code, notes, and snippets.

@putheakhem
Created November 21, 2017 15:39
Show Gist options
  • Save putheakhem/42aab5558ebbb3c8c5904ce6ebc37df4 to your computer and use it in GitHub Desktop.
Save putheakhem/42aab5558ebbb3c8c5904ce6ebc37df4 to your computer and use it in GitHub Desktop.
HTML DOM setAttribute() Method
<!DOCTYPE html>
<html>
<head>
<style>
.democlass {
color: red;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<p>Click the button to add a class attribute with the value of "democlass" to the h1 element.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementsByTagName("h1")[0].setAttribute("class", "democlass");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment