Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting background image using js array

Status
Not open for further replies.

gwar2k1

Programmer
Apr 17, 2003
387
GB
i have an array that randomly picks an image on every load of the page... array works fine but i need to set the image as a background for a div:

<div class=&quot;main&quot;>
<script language=&quot;javascript&quot;>
document.write('TheImage')
</script>
</div>

that'd suffice BUT the image is out of line with the div some how =S
So i tried:

<script language=&quot;javascript&quot;>
document.write('<div class=&quot;main&quot; style=&quot;background-image: '++TheImage++';&quot;>)
</script>
</div>

but that produces an error. can any one help me please?

~*Gwar3k1*~
&quot;To the pressure, everything's just like: an illusion. I'll be losing you before long...&quot;
 
Not tested, But try

document.write('<div class=&quot;main&quot; style=&quot;background-image:url(<Insert Image Path>);&quot;>)

Replace <Insert Image Path> with the path to your image:

url(images/myimage.jpg);


MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
darn it i cant believe i forgot to add &quot;url&quot; oops but im now using this and its producing an error

document.write('<div class=&quot;main&quot; style=&quot;background-image: url('++TheImage++');&quot;>')

~*Gwar3k1*~
&quot;To the pressure, everything's just like: an illusion. I'll be losing you before long...&quot;
 
Try:

document.write('<div class=&quot;main&quot; style=&quot;background-image: url('+TheImage+');&quot;>')

Get rid of the Extra Plus signs.

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
k thanks :D

~*Gwar3k1*~
&quot;To the pressure, everything's just like: an illusion. I'll be losing you before long...&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top