Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

W3C validation fails - fixing it breaks javascript

Status
Not open for further replies.

squibs

Technical User
Sep 24, 2007
10
I'm using a simple javascript routing for button rollovers. The rollover images get preloaded:
Code:
image1 = new Image();
image1.src = "/images/image1blue.jpg";
And the buttons are implemented like this:
Code:
   <a href="/index.php" onmouseover="image1.src='/images/image1blue_2x1.jpg';"
   onmouseout="image1.src='/images/image1black.jpg';">
   <img name="image1" src="/images/image1black.jpg" /></a>
Unfortunately this will not validate for xhtml strict as the name tag is unknown. If I change to
Code:
<img id="image1"....
the code breaks in internet explorer. Can anybody tell me how to get around this? It is important that the code validates.
 
Using the ID attribute, try this:
Code:
onmouseover="document.getElementById('image1').src='/images/image1blue_2x1.jpg';"

Lee
 
Or better yet, instead of putting an <img> tag inside your <a> tag, define the image as the background-image of the <a> using css - and then use the :hover pseudoclass to perform the onmouseover and onmouseout actions. There's no need to use javascript for this because it's a built-in functionality for anchors on pretty much every modern browser.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Thanks for the replies. For some reason the css idea never occurred to me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top