Why isn't my javascript code to change the background images working?

<HTML><HEAD> <SCRIPT language="JavaScript"> <!-- var arPics = new Array(1) arPics[0]="Folder 1"; function changeImg() { var objSelect = document.getElementById("selChoice") var objImg1 = document.getElementById("bg1") var objImg2 = document.getElementById("bg2") if (objImg1 && objSelect) { objImg1.background = "Images/" + arPics[objSelect.options[objSelect.selectedIndex].value] + "/backmain.jpg"; objImg2.background = "Images/" + arPics[objSelect.options[objSelect.selectedIndex].value] + "/backtable.jpg"; } else { alert("Error: 0010"); }}//--> </SCRIPT></head><body id="bg1" background="" onload="changeImg(0)"> <center><table width="100%" border="1"> <tr><td id="bg2" background=""><center> form to choice background here (couldnt fit code) </center></tr></table></body></html> Probably doesn't make sense what im trying to do but thats because i couldn't fit all the code. Anyway for some reason the table background (bg2) wouldn't display at all, but the body background (bg1) will. The file i took this from does have text in the cell, and the background still doesn't display. I would have posted the whole file, but yahoo does not let me because its too many characters. Any other ideas why this wouldn't be working?

Public Comments

  1. Put some text in the table cell where you want the background image to show up(Right after your '<td id="gb2" ....>' tag, put some text and see what happens. I believe the reason you don't see a background image is because the cell doesn't have any text to show so its size is 0 pixels vertically, thus not showing background image.
  2. I see lots of problems! 1) we don't use a language parameter on script tags! we say type="text/javascript" 2) we don't make arrays with new Array(1)... they are flexible and don't need to have a size 4) although you got the correct elements, you are assigning the background wrong (that's the big problem) obj.style.backgroundImage = 'url(blabla.jpg)' works the way you want it to! 5) we don't use center tags, we use css! Good luck and good programming!
Powered by Yahoo! Answers