Skip to main content

Posts

Showing posts with the label CSS

Set div background image from javascript

While working with JavaScript and html many time this happens that you need to set some CSS property via  JavaScript  .One of the scenario that I faced was setting div's background image via a  JavaScript  function. I had following code : <div id="tree" style="width:200px; height:150px;">    Test data  </div> Now while loading the page I had few condition depending on that it should set the background image of the div.So, I created a function in JavaScript and called it onLoad of the body tag in the html page. That JavaScript method is as following : function changeDivBackground() {      document.getElementById("tree").style.background = "url('images/backgroundImage.jpg')"; } This will set the background image for the div.