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...

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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..

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.