We help IT Professionals succeed at work.

Image Rollover and OnClick

rajesh75
rajesh75 asked
on
Medium Priority
744 Views
Last Modified: 2012-05-05
Hi,

I have this onClick and mouseover script for a set of images with a "on" and "off" state.

The problem I have is that although the onClick works and it changes the image's "off" state to "on", I always need to right click and click "Show Picture" to make the image visible.

Why would this happen?

I am using "eval" to dynamically find the image and change the src.

Thanks for any help in advance.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
Can you post your code?

Author

Commented:
Javascript:


function BOGo(id) {

      switch (id)
                  {      case "car" :
                              flg = "car";
                              break;
                        case "hotels" :
                              flg = "hotels";
                              break;
                        case "vac" :
                              flg = "vac";
                              break;
                        case "lastmin" :
                              flg = "lastmin";
                              break;
                                          }
                  
      eval("document." + flg + ".src = \"./Images/admod_" + flg + "_on.gif\"");
}

Calling function:

<A onclick="BOGo('vac');" href="javascript:void(0);"><Img src="./Images/admod_vac_off.gif" border="0" name="vac" border=0>

Thanks.
CERTIFIED EXPERT
Top Expert 2004
Commented:
isn't this easier?

    function BOGo( tImg )
    {
         tImg.src = './Images/admod_' + tImg.id + '_on.gif' ;
    }

    <img src="Images/admod_vac_off.gif" border="0" id="vac" border="0" onclick="BOGo( this )">

Tim

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Tim. This is a cross browser script right? Would this work on some of the older versions of Netscape and IE?

Thanks again.
CERTIFIED EXPERT
Top Expert 2004

Commented:
as far as I know...  I've not been doing javascript long though...  so I may be wrong :-/

Author

Commented:
I'd like to test this script on a few older browsers and if everything goes well, you got the points !

Thanks.
CERTIFIED EXPERT
Top Expert 2004

Commented:
Good luck..it shoud work (as far as I know), as its not really doing anything fancy :-)

*crosses fingers*

Tim
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.