+ 1

How to change background color by JavaScript ?

I am making a code that will change the background color of the <body> element with JavaScript. I have tried the ways below but failed. Please let me know if you know any other ways to do it. • document.body.style.backgroundColor="red"; • document.getElementById("body").backgroundColor="red"; • document.getElementsByTagName("body").setAttribute("style","background-color:red"); Tell me if my code is wrong or it is a defect of sololearn app ?

6th Jun 2019, 7:36 AM
Nasir Uddin Vuiya
Nasir Uddin Vuiya - avatar
1 Respuesta
+ 6
There is really easy way to do it. You don't need to use setAttribute for it. Like this, Add id of "body" to body tag and then, var a = document.getElementById("body'"); a.style.backgroundColor = "blue";
6th Jun 2019, 7:56 AM
Raj Chhatrala
Raj Chhatrala - avatar