Is it possible to dynamically change the value of a background in an external CSS file using JavaScript? Currently, all my pages are connected to a CSS file that controls the background for each page. I am attempting to modify the background by clicking a button with this code:
html:
<button style="background:url(images/bg.png);" onclick="main()"><font color="#FFFFFF">Image</button>
<button style="background-color:#000;" onclick="main()"><font color="#FFFFFF">Black</button>
<button style="background-color:#030;" onclick="main()"><font color="#FFFFFF">Green</button>
<button style="background-color:#006;" onclick="main()"><font color="#FFFFFF">Blue</button>
css:
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
font-size: 24px;
background:url(images/bg.png);
}
Javascript:
<script type="text/javascript">
function main(clicked){
//Doesnt work
document.styleSheets[0].cssRules[0].style.background="#00FF00";
}
</script>
How can I achieve changing the background in the css file through the click of a button?