Javascript DOM CSS Style object get from an element

Introduction

Accessing a specified element's style object:

var x = document.getElementById("myH1").style;

Click the button to get the style property of the H1 element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<h1 id="myH1" style="color:red">My Header</h1>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from  w  w w.ja  v a2s  .  c  o m
  var x = document.getElementById("myH1").style.color;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related