Javascript DOM HTML Element style Property set color

Introduction

The style property returns a CSSStyleDeclaration object, which represents an element's style attribute.

Add a red color to an <h1> element:

document.getElementById("myH1").style.color = "red";

Click the button to add a color to the H1 element.

View in separate window

<!DOCTYPE html>
<html>
<body>

<h1 id="myH1">How to change the style of a header</h1>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//ww  w  .j av  a 2  s.  c o  m
  document.getElementById("myH1").style.color = "red";
}
</script>

</body>
</html>

Element style Property returns a CSSStyleDeclaration object representing an element's style attribute.




PreviousNext

Related