Javascript CSSStyleDeclaration setProperty Method with "important" priority

Introduction

Set a new CSS property with "important" priority:

Click the button to set a new CSS property with "important" priority.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#ex1 {/*from w w w  .  ja va  2s.  c  o  m*/
  color: red;
}
</style>
</head>
<body>
<button onclick="myFunction()">Test</button>
<div id="ex1">Some text.</div>

<script>
function myFunction() {
  var declaration = document.styleSheets[0].cssRules[0].style;
  var setprop = declaration.setProperty("background-color", "yellow", "important");
}
</script>

</body>
</html>



PreviousNext

Related