Edit This Code:
<!DOCTYPE html>
<html>
<head>
<style>
div {
    width: 400px;
    height: 400px;
    border: 1px solid black;
}

</style>
</head>
<body>

<h1>Hello World!</h1>

<div id="myDiv">This is a div element.</div>
<br>
<button type="button" onclick="myFunction()">Set the background image of div</button>

<script>
function myFunction() {
    document.getElementById("myDiv").style.backgroundImage = "url('img_tree.png')";
}
</script>

</body>
</html>


Result:
Try it Yourself - © w3schools.com
Privacy Policy