how to change background color when click on button in javascript code example
Example 1: javascript change background color
function changeBackground(color) {
document.body.style.background = color;
}
window.addEventListener("load",function() { changeBackground('red') });
Example 2: change background color on button click in javascript
<style>
.bg1 {
background-color: red;
}
</style>
<button id="btnChangebgColor" />
<script>
$(document).ready(function(){
$("#btnChangebgColor").click(function() {
$(this).addClass(".bg1");
});
}
</script>