javascript change background code example

Example 1: javascript change background color

document.body.style.backgroundColor = "yellow";

Example 2: JavaScript Change Background Color

var element = document.body;
element.style.backgroundColor = "blue";

Example 3: change background with js

document.body.style.backgroundColor = "red";

Example 4: how to set background colour i js inline stylel

<!DOCTYPE html>
<html>
   <head>
      <title>HTML Backgorund Color</title>
   </head>
   <body style="background-color:grey;">
      <h1>Products</h1>
      <p>We have developed more than 10 products till now.</p>
   </body>
</html>

Example 5: change background with js

document.body.style.backgroundImage = "url('img_tree.png')";

Example 6: change background with js

document.body.style.backgroundPosition = "top right";