Computer >> Computer tutorials >  >> Programming >> Javascript

How to set the background image for an element with JavaScript DOM?


To set the background image in JavaScript, use the backgroundImage property. It allows you to set the background image.

Example

You can try to run the following code to learn how to set the background image with JavaScript −

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Click to Set background image</button>
      <script>
         function display() {
            document.body.style.backgroundImage = "url('https://www.tutorialspoint.com/html5/images/html5-mini-logo.jpg')";
         }
      </script>
   </body>
</html>