Javascript DOM CSS Style background Property set

Introduction

Change the background of a DIV element:

document.getElementById("myDIV").style.background = "url('background2.png') blue repeat-x center";

Click the button to change the background property of the DIV element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//from w ww. ja v a 2 s  . co m
  width: 500px;
  height: 500px;
  background: coral url('background2.png') no-repeat fixed center;
  color: white
}
</style>
</head>
<body>
<button onclick="myFunction()">Test</button>

<div id="myDIV">
  <p>My DIV element</p>
</div>

<script>
function myFunction() {
  document.getElementById("myDIV").style.background = "url('background2.png') lightblue repeat-x center";
}
</script>

</body>
</html>



PreviousNext

Related