Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
hackerss
hackerss

Posted on

Change background color of a div using the id in Javascript

Change background color of a div using the id - Javascript:


//function to change background color of a div
function changeBackgroundColor(id) {
  //get the div with the id
  const div = document.getElementById(id);
  //change the background color of the div to red
  div.style.backgroundColor = "red";
}

//example
changeBackgroundColor("div1");


Enter fullscreen mode Exit fullscreen mode

Top comments (0)