Title: Using javascript to set .style.backgroundImage
Last modified: August 30, 2016

---

# Using javascript to set .style.backgroundImage

 *  Resolved [Peter](https://wordpress.org/support/users/peterkaagman/)
 * (@peterkaagman)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/using-javascript-to-set-stylebackgroundimage/)
 * Hi there forum,
 * I’m trying to use javascript to set the style property backgroundImage. But I
   can’t seem to get it working… and worst… a bit clueless at the moment as for 
   the reason
 * The Div is declare in header.php:
 *     ```
       <Div id="site-logo"></Div>
       ```
   
 * Style.css has some settings for that div:
 *     ```
       #site-logo{
               position: absolute;
               right: 0;
               z-index: 10;
               width: 300px;
               height:300px;
               background-size: 100% 100%;
               background: red;
       }
       ```
   
 * The red background is for debugging purposes… sticks out to identify visibility
   and positioning.
 * and I’m using the following javascript:
 *     ```
       var howOften = 5; //number often in seconds to rotate
       var current = 0; //start the counter at 0
   
       // place your images, text, etc in the array elements here
       var items = new Array();
           items[0]="url=('/wp-content/uploads/images/site-logo-dame1.png')";
           items[1]="url=('/wp-content/uploads/images/site-logo-there-is-only-2.png')";
   
         function rotater() {
           document.getElementById("site-logo").style.backgroundImage = items[current];
           current = (current==items.length-1) ? 0 : current + 1;
           setTimeout("rotater()",howOften*1000);
         }
       window.onload=rotater;
       ```
   
 * Using a javascript alert() I made sure the code gets executed and checked the
   value assigned to items[current].
    This same script did work when I used the 
   innerHTML property.

Viewing 1 replies (of 1 total)

 *  Thread Starter [Peter](https://wordpress.org/support/users/peterkaagman/)
 * (@peterkaagman)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/using-javascript-to-set-stylebackgroundimage/#post-6320123)
 * this
 *     ```
       // place your images, text, etc in the array elements here
       var items = new Array();
           items[0]="url=('/wp-content/uploads/images/site-logo-dame1.png')";
           items[1]="url=('/wp-content/uploads/images/site-logo-there-is-only-2.png')";
       ```
   
 * should have been
 *     ```
       // place your images, text, etc in the array elements here
       var items = new Array();
           items[0]="url('/wp-content/uploads/images/site-logo-dame1.png')";
           items[1]="url('/wp-content/uploads/images/site-logo-there-is-only-2.png')";
       ```
   
 * the equel sign should not have been there

Viewing 1 replies (of 1 total)

The topic ‘Using javascript to set .style.backgroundImage’ is closed to new replies.

## Tags

 * [background+image](https://wordpress.org/support/topic-tag/backgroundimage/)
 * [css](https://wordpress.org/support/topic-tag/css/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * 1 reply
 * 1 participant
 * Last reply from: [Peter](https://wordpress.org/support/users/peterkaagman/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/using-javascript-to-set-stylebackgroundimage/#post-6320123)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
