Skip to content

Instantly share code, notes, and snippets.

@JokingChicken
Last active July 12, 2018 19:46
Show Gist options
  • Save JokingChicken/44e953c9d0014e5c1daf9f58670413d1 to your computer and use it in GitHub Desktop.
Save JokingChicken/44e953c9d0014e5c1daf9f58670413d1 to your computer and use it in GitHub Desktop.
Modifying a stylesheet rule with CSSOM
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Using_dynamic_styling_information -->
<html>
<head>
<title>Modifying a stylesheet rule with CSSOM</title>
<style type="text/css">
.test {
background-color: red;
}
test2 {
background-color: red;
}
</style>
<style type="text/css">
body {
background-color: red;
}
</style>
<script type="text/javascript">
var stylesheet = document.styleSheets[2];
console.log(stylesheet);
stylesheet.cssRules[0].style.backgroundColor="blue";
</script>
</head>
<body>
The stylesheet declaration for the body's background color is modified via JavaScript.
<br>
<test2>test</test2>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment