element.setAttribute("style", "background-color: blue;");
element.setAttribute.backgroundColor = "blue";
element.setAttribute(attributeName, attributeValue)
<html>
<head>
<title> JavaScript setAttribute() method </title>
<script>
function fun() {
document.getElementById("link").setAttribute("href", "https://bianchenghao6.com/");
}
</script>
</head>
<body style = "text-align: center;">
<p> It is an example of adding an attribute using the setAttribute() method. </p>
<a id = "link"> bianchenghao6.com </a>
<p> Click the follwing button to see the effect. </p>
<button onclick = "fun()"> Add attribute </button>
</body>
</html>
<html>
<head>
<title> JavaScript setAttribute() method </title>
<script>
function fun() {
document.getElementById("change").setAttribute("type", "button");
}
</script>
</head>
<body>
<p> It is an example to update an attribute's value using the setAttribute() method. </p>
<input id = "change" type = "text" value = "lidihuo"/>
<p> Click the follwing button to see the effect. </p>
<button onclick = "fun()"> Change </button>
</body>
</html>
Click the follwing button to see the effect.
<html>
<head>
<title> JavaScript setAttribute() method </title>
<script>
function fun() {
document.getElementById("btn").setAttribute("disabled", "");
}
</script>
</head>
<body>
<p> Example of the setAttribute() method. </p>
<p> Click the following button to see the effect </p>
<button onclick = "fun()" id = "btn"> Click me </button>
</body>
</html>
It is an example to update an attribute's value using the setAttribute() method.
Click the follwing button to see the effect.