Jump to content

simple change div image background can't make it work :(


adredz

Recommended Posts

Have a div that already changes its background onmouseover and onmouseout which was done through simple CSS. Now I want to add an effect that when the div gets clicked it retains the image used onmouseover. I implemented it via javascript but I can't seem to make it work. Here's my code:

div html:

<div id="content_1_1"><a href="javascript:changeBg('content_1_1');"> </a> nothing</div>

 

js function:

function changeBg(content){

document.getElementById('content').style.backgroundImage = "url('images/itembg_on.jpg')";

}

 

any help would be appreciated...

your code should be

 

function changeBg(content){
   document.getElementById('content').style.backgroundImage = "url(images/itembg_on.jpg)";
}

 

also your anchor should contain some text it will be easier to click

<div id="content_1_1"><a href="javascript:changeBg('content_1_1');"> nothing </a></div>

 

 

It's not working..

Tried removing the  ''  from the getElemeentById parameter:

 

function changeBg(content){

document.getElementById(content).style.backgroundImage = "url(images/itembg_on.jpg)";

}

 

And replaced the function call method through the onlick attribute of div:

<div id="content_1_1" onclick="changeBg(this.id)">

 

It does have an effect, but it removes the bg instead of replacing it. It has successfully called the function right? If so what's the problem? The image path is certainly /images/itembg_on.jpg..

 

I think this has something to do with wordpress since I had trouble implementing the same thing for my dynamic nav menu. I had to host the image to the internet to get the desired effect. I think may have found a solution while writing this... :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.