The value
property sets or gets the value of the attribute.
value |
Yes | Yes | Yes | Yes | Yes |
Set the attribute value
attribute.value = value
Return the attribute value
attribute.value
A String representing the value of the attribute.
The following code shows how to get the value of an attribute:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!-- w w w .j a v a 2 s .c om-->
<p id="demo"></p>
<script>
function myFunction() {
var btn = document.getElementsByTagName("BUTTON")[0];
var x = document.getElementById("demo");
x.innerHTML = btn.attributes[0].value;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to set the style attribute for a header Element.
<!DOCTYPE html>
<html>
<body>
<!--from w ww . j av a 2 s .c o m-->
<h1 style="color:red;">Hello World</h1>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
var h = document.getElementsByTagName("H1")[0];
h.getAttributeNode("style").value = "color:green;background-color:black;";
}
</script>
</body>
</html>
The code above is rendered as follows: