Title: javascript function to set body background from custom  value
Last modified: August 19, 2016

---

# javascript function to set body background from custom value

 *  [pressnewb1](https://wordpress.org/support/users/pressnewb1/)
 * (@pressnewb1)
 * [17 years, 9 months ago](https://wordpress.org/support/topic/javascript-function-to-set-body-background-from-custom-value/)
 * Hi,
    I’m trying to use an image name as a post custom value (e.g img1.jpg) for
   a page and want to pass it as an array variable to a JavaScript function, to 
   replace the background image set in the stylesheet but it doesn’t seem to be 
   working.
 * Here’s my code (in the header.php):
 *     ```
       <script type="text/javascript">
   
       function changeBackground(whichImage) {
       if(document.body){
       document.body.style.backgroundImage = "url(whichImage)";
       }
       </script>
       </head>
   
       <?php $values = get_post_custom_values("bgimage") ?>
       <body onLoad="changeBackground($values[0])">
       </body>
       </html>
       ```
   
 * It could be something really obvious but I’m missing it.
    Help!!

The topic ‘javascript function to set body background from custom value’ is closed
to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [pressnewb1](https://wordpress.org/support/users/pressnewb1/)
 * Last activity: [17 years, 9 months ago](https://wordpress.org/support/topic/javascript-function-to-set-body-background-from-custom-value/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
