The setAttribute()
method adds the attribute and value to an element.
DOM Element setAttribute |
Yes | 9 | Yes | Yes | Yes |
element.setAttribute(attributename, attributevalue)
Parameter | Type | Description |
---|---|---|
attributename | String | Required. The name of the attribute |
attributevalue | String | Required. The value of the attribute |
No return value.
The following code shows how to set the type attribute of an input element.
<!DOCTYPE html>
<html>
<body>
<!-- w w w.j ava 2 s . c om-->
<input value="OK">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementsByTagName("INPUT")[0].setAttribute("type", "button");
}
</script>
</body>
</html>
The code above is rendered as follows: