Javascript DOM CSS Style backgroundImage Property get

Introduction

Return the background image of a specific <div> element:

alert(document.getElementById("myDiv").style.backgroundImage);

View in separate window

<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>

<div id="myDiv" style="background-image:url('background1.png');border:1px solid black;height:400px;width:400px;">This is a div element.</div>
<br>
<button type="button" onclick="myFunction()">Get the background image of div</button>
<p id="demo"></p>
<script>
function myFunction() {/*from  w ww.j  a  va2s .c om*/
  document.getElementById("demo").innerHTML = document.getElementById("myDiv").style.backgroundImage;
}
</script>

</body>
</html>



PreviousNext

Related