HTML Tutorial and Reference in HTML/XHTML Posted January 13, 2021 Hi Can I use a function like this to change the background of a body tag that has a class instead of an ID? <body id="cbg"> <button onclick="changeBG()">Change Background</button> <script> function changeBG(){document.getElementById('cbg').style.background = "repeating-radial-gradient(#444 0%,#222 10%,#444 40%) 0 / 12px 12px"} </script> ----------------------------------------------------- <body class="cbg"> <button onclick="changeBG()">Change Background</button> <script> function changeBG(){document.getElementByClassName('cbg').style.background = "repeating-radial-gradient(#444 0%,#222 10%,#444 40%) 0 / 12px 12px"} </script>
HTML Tutorial and Reference
in HTML/XHTML
Posted
Hi
Can I use a function like this to change the background of a body tag that has a class instead of an ID?
<body id="cbg">
<button onclick="changeBG()">Change Background</button>
<script>
function changeBG(){document.getElementById('cbg').style.background = "repeating-radial-gradient(#444 0%,#222 10%,#444 40%) 0 / 12px 12px"}
</script>
-----------------------------------------------------
<body class="cbg">
<button onclick="changeBG()">Change Background</button>
<script>
function changeBG(){document.getElementByClassName('cbg').style.background = "repeating-radial-gradient(#444 0%,#222 10%,#444 40%) 0 / 12px 12px"}
</script>