SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a nubie question

    What am I doing wrong?


    Code:
    var backgrounds = new Array("images/tiles/pattern_044.gif","images/tiles/pattern_045.gif","images/tiles/pattern_046.gif");
    
    // setEvents is triggered by onload in the body tag
    function setEvents() {   
    changeBkg();
    }
    
    function changeBkg() {
      if (document.body) {
        alert(backgrounds[0]);  //this gives me images/tiles/pattern_044.gif as expected
    //  document.body.style.backgroundImage="\"url("+'+backgrounds[0]+'+")\"";     // this didn't work
    //	document.body.style.backgroundImage="url('backgrounds[0]')";               // this didn't work
     	document.body.style.backgroundImage="url('images/tiles/pattern_044.gif')"; // this works but I want to use the array
      }
    }
    all I want to do is set backgroungImage using an item from the array

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Dartmouth, MA, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the first commented-out line, but get rid of the single quotes around backgrounds[0]. By putting the quotes around it, JavaScript will treat it as if you really want the name of the file to be used for the background to be 'backgrounds[0]'. Instead, put the opening single quote right after the opening parenthesis, and the closing single quote right before the closing parenthesis (within the double quotes). Also, I don't think you need the extra pair of escaped double quotes in that string.

  3. #3
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're a genius!!! this worked "url("+backgrounds[0]+")"
    and I was sure I already tried it that way.
    thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •