www.webdeveloper.com
Results 1 to 8 of 8

Thread: javascript to change anchor background image

  1. #1
    Join Date
    Mar 2005
    Posts
    32

    javascript to change anchor background image

    Just as the title says. Is there a javascript, script, that changes the background image / or the style of an anchor? Been looking everywhere.

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    <a href="#" onmouseover="this.style.background='#000 url(image2.gif)';" style="background:#000 url(image1.gif)">xxxx</a>

  3. #3
    Join Date
    Mar 2005
    Posts
    32
    nope, not on mouseOver, permantly. I've got a CSS and script working that has one image for when it's normal (a:link, a:visited), one image for mouse over (a:hover) and one for when clicked (a:active), what I want to happen is when it's clicked, the a:link and a:visited change to the a:active and vice-versa, I just don't know the syntax.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Swap the class:
    Code:
    <a href="#" class="foo" onclick="this.className=(this.className=='foo')? 'bar' : 'foo';">xxxx</a>

  5. #5
    Join Date
    Mar 2005
    Posts
    32
    Yeah, that's what I was thinking alright, cheers.

  6. #6
    Join Date
    Mar 2005
    Posts
    32
    K, I wanna do that alright, but not in that way, it won't work for how I want it. Here's a code snippit:

    HTML:
    <a id="butRoute1" className="unpressed" href="javascript:restore('butRoute1')></a>

    javascript:
    var button;
    function restore(button) {
    y = document.getElementById(button).className;

    if (y == 'unpressed') {
    y = pressed;
    }
    else {
    y = 'unpressed';
    }
    }

    I've got all the CSS how I want it and I've got one button starting with the class pressed and one starting as unpressed, but they're just not switching, any idea why?

  7. #7
    Join Date
    Mar 2005
    Posts
    32
    'sokay, it was cos I was using ".className", I used .setAttribut('class','pressed) instead and it worked a charm, thanks anyway.

  8. #8
    Join Date
    Dec 2009
    Posts
    1
    DarkoZiac, can you post the whole code here.I need to change permanently the anchor background image onclick too but I don't know javascript very well

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles