*
The moose likes HTML, CSS and JavaScript and the fly likes changing link background image dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "changing link background image dynamically" Watch "changing link background image dynamically" New topic
Author

changing link background image dynamically

david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
Hi,

I have a few links on a web page, each of link has a background image, i want to change the background image when the link is clicked , related code as below:


First, I tried to use javascript to change the link class(method 2), then I tried to change the link background image directly(method 1), I also tried to define the link active through css, all of them does not perform the task I need. Please help to figure out the problem.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

david arnold wrote:
document.getElementById(linkID).className = "title_link_error_no_select";

There is no such class in your (shown) CSS.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
oh, i forgot to to include that in the post, it is similar to the others except it has different background image.



Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

So, what's the next debugging step? For example, have you 100% verified that the expression document.getElementById(linkID) references the expected element?

And, you do realize that assigning to className replaces all other classes, right?

david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
Thank you Bear.

Yeah, document.getElementById(linkID) point to the right element, and I just tried


Which works, without the resetting the attribute innerHTML, both does not work. I do not know why.

I am not very sure if setting the className to that element will replace the old one, now I am sure after your informing .

Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: changing link background image dynamically