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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change div bg-image onclick

Status
Not open for further replies.

jasinner

Technical User
Feb 15, 2006
5
US
I'm trying to find a way to change the background-image of a div when certain elements are clicked. Here is what I have but it isn't working:

...
#bgimage {
position:absolute;
background-image: url(1.jpg);
top:10px;
left: 220px;
width: 767px;
height: 549px;
}
...

<img src="1thumb.jpg" style="position:absolute;left:10px;top:10px;" onclick="bgimage.style.background-image:url(1.jpg);" />
<img src="2thumb.jpg" style="position:absolute;left:10px;top:213px;" onclick="bgimage.style.background-image:url(2.jpg);" />
<img src="3thumb.jpg" style="position:absolute;left:10px;top:416px;" onclick="bgimage.style.background-image:url(3.jpg);" />

<div id="bgimage">
</div>
 
Your code has a minus sign in it, which Javascript will choke on, among other things:
Code:
onclick = "bgimage.style.background[red]-[/red]image:url"(1.jpg);"

Try this
Code:
onclick = "document.getElementById('bgimage').style.backgroundImage='1.jpg';"

Lee
 
I think you might need the "url()" portion, too (from memory, so could be wrong):

Code:
onclick = "document.getElementById('bgimage').style.backgroundImage = 'url(1.jpg)';"

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top