How to set background color of HTML element using css properties in JavaScript
I'm trying to set the background color of an HTML element using CSS properties in JavaScript, but I'm facing some issues.
I have a <div> element with the ID myElement and I want to change its background color dynamically using JavaScript.
I know I can do this using the style property, but I'm not sure how to specify the background color using CSS properties. Can someone help me with this?
save
listen
AI Answer
How to set background color of HTML element using css properties in JavaScript
1
I'm trying to set the background color of an HTML element using CSS properties in Jav…
asked
Apu
1 answers
2915
I'm trying to set the background color of an HTML element using CSS properties in Jav…
Answer Link
answered
Apu
const myElement = document.getElementById("myElement");
myElement.style.backgroundColor = "red";
This code will set the background color of the element with the ID myElement to red. You can replace red with any valid CSS color value like "blue", "#00ff00", or "rgb(255, 0, 0)".