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

HTMLJavascriptProgramming Scripts

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 −

Live Demo

<!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>
raja
Published on 27-Feb-2018 05:09:39
Advertisements