Solved

Rollover JavaScript not working in Safari and Chrome

Posted on 2011-09-07
8
453 Views
Last Modified: 2012-05-12
I have a page that displays a thumbnail image when you roll over a link.


The JavaScript routines for rollove rand rollout are simply:

<script type="text/javascript" language="javascript">

function showImage(image) {
    image.style.visibility="visible";
}
   
function hideImage(image) {
    image.style.visibility="hidden";  
}
 </script>

And the HTML is:

<img id="sib_1_tn" class="thumb" src="images/Siblings/sib_1_tn.jpg" />
<a href="sib_1.php" onmouseover="showImage(sib_1_tn)" onmouseout="hideImage(sib_1_tn)">Sibling 1 </a>
 

=================

This works fine in IE and Firefox but neither Chrome nor Safari display the thumbnail image when you roll over the link.  Does anyone run an idea what the problem might be?

Thanks for any suggestions.

Steve
0
Comment
Question by:steva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36499984
put the ID of the image inside simple quote :

onmouseover="showImage('sib_1_tn')" onmouseout="hideImage('sib_1_tn')
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36499995
<img id="sib_1_tn" class="thumb" src="images/Siblings/sib_1_tn.jpg" />
<a href="sib_1.php" onmouseover="showImage('sib_1_tn')" onmouseout="hideImage('sib_1_tn')">Sibling 1 </a>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36500030
an update to the function in this case : http://jsfiddle.net/QXJWn/

function showImage(image) {
    document.getElementById(image).style.visibility="visible"; 
}
    
function hideImage(image) {
    document.getElementById(image).style.visibility="hidden";  
}

Open in new window

0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36500036
0
 

Author Comment

by:steva
ID: 36500427
Thanks Leakim.  The basic problem was that I was just using the passed in ID to access the DOM element (image.style.visibility="visible")  instead of going through getElementById().  For some reason Firefox and IE let you get away with  this, but not Safari and Chrome.

Thanks.  I gave you the points.

Steve
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36500462
Hi Steve,

What's up? Yes, you used shorcut references for your fields.
It's a good pratice to use :

document.getElementsByName("NAME_ATTRIBUTE")[0]
or :
document.getElementById("ID_ATTRIBUTE")

please note the << [0] >> on the first one and the << s >> after element
You can have multiple fields with the same name in a page ( [0] for the first one, [1] for the second and so on )

ID should/MUST be unique so there's no getElementsById

Thanks for the points
Best Regards
0
 

Author Comment

by:steva
ID: 36503053
Got it!  Thanks.

(I'm fine. When I'm not doing web pages for people  I'm working my "software" through  Microsoft's "Certified For" program to give it credibility.  I hope all is well with you.).
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36505178
very nice, good luck, don't forget the fun
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question