Subscribe to my RSS feed RSS
July 28, 2008

How do I use onMouseOver in Javascript?

  • Written by under web development |
  • 1 comment so far |

I want an image on my website to change when I hover my mouse over it. How do I do this?


I got this question from a friend who like to dabble with HTML but isn’t too much f an expert. He saw this effect on a button on a travel insurance website was trying to produce something similar. Well, you cannot implement this with plain HTML, but need to delve into Javascript and use a function called onMouseOver. This function is triggered when the mouse runs over a certain element on your page. It also has a corresponding sister function called onMouseOut which is called when you move your mouse away from the element in question. It’s important to use both functions otherwise your image won’t revert back to the original when the mouse moves away.

So, here’s the code that lets you do this:
<html>
<head>
<script type="text/javascript">
function mouseOver() {
document.b1.src ="image2.jpg";
}
function mouseOut() {
document.b1.src ="image1.jpg";
}
</script>
</head>
<body>
<a href="http://askowen.info" target="_blank" onmouseover="mouseOver()" onmouseout="mouseOut()">
<img border="0" alt="Ask Owen" src="image1.jpg" name="b1" /></a>
</body>
</html>

And here’s what it looks like:

I haven’t actually linked the image to anything, but you get the idea ;)

    Related Questions

    • Can I turn off Javascript in Adobe Reader?
      I have read news online of security problems with Adobe Reader due to Javascript. Can I turn this off? I had to do some reading up on this to tell you the truth, as I had no idea that Adobe Reader ...
    • How do I create an alternative to Javascript?
      I'm coding this new left navigation menu for a new website site, and I want to be able to have the navigation show if the visitor has Javascript turned off. Unfortunately the menu has sub-menus that d...
    • How do I add a Facebook badge to by blog?
      I'd like to add a Facebook badge to my blog similar to what I've seen on other websites. Where do I get the code from? I was asked this question by Derek Richardson, a friend of mine who was look...
Feed for this Entry | Trackback Address

1 comment so far

  1. mike on 10.18.2009 at 10:35 am | permalink
  2. can you do it with 3 images and continuously rotating while mouseover?

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>