HTML DOM createAttribute()
This createAttribute() function makes a new attribute when you say what it should be called. You can use attribute.value to decide what's inside it. To put this new attribute in an element, you can use element.setAttribute() by telling it the attribute's name.
Syntax:
document.createAttribute( attributename )
<!DOCTYPE html>
<html>
<head>
<style>
.gfg {
color: green;
font-weight: bold;
font-size: 50px;
}
</style>
<title>Page Title</title>
</head>
<body>
<h1> Propertutorials </h1>
<h2> DOM createAttribute() Method </h2>
<button onclick="myfunc()"> Submit </button>
<script>
function myfunc() {
let tag_name = document.getElementsByTagName("h1")[0];
let attr = document.createAttribute("class");
attr.value = "gfg";
tag_name.setAttributeNode(attr);
}
</script>
</body>
</html>