Javascript DOM HTML Element style Property access <style> element

Introduction

Access the <style> element from <head> by using document.getElementsByTagName():

Click the button to display the style properties of this document.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
body {/*www .  jav a  2s  .  com*/
  background-color: yellow;
  color: red;
  font-size: 15px;
}
</style>
</head>
<body>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  var x = document.getElementsByTagName("STYLE")[0];
  document.getElementById("demo").innerHTML = x.innerHTML;
}
</script>

</body>
</html>



PreviousNext

Related