# Arrgh, need simple tutorial for javascript mouseover for changing image.

**URL:** https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807
**Category:** Factual Questions
**Created:** [May 5, 2011, 4:19am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807 "2011-05-05T04:19:50Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 5, 2011, 4:19am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/1 "2011-05-05T04:19:50Z")

</div>

I used to be able to do this. Now I google and every tutorial is telling me to use different code and none of it is working. Arrgh. Using hostgator and cpanel.

---

<div class="post-metadata">

### Author: ![GuanoLad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/guanolad/32/42_2.png) [@GuanoLad](https://boards.straightdope.com/u/GuanoLad)
#### Post date: [May 5, 2011, 4:22am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/2 "2011-05-05T04:22:50Z")

</div>

You can do most hover effects with CSS, these days. Javascript for something so trivial is not usually necessary.

Though it depends on what you’re using it for.

---

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 5, 2011, 4:38am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/3 "2011-05-05T04:38:27Z")

</div>

> [@GuanoLad](#):
>
> You can do most hover effects with CSS, these days. Javascript for something so trivial is not usually necessary.
> 
> Though it depends on what you’re using it for.

Have a banner that I want to highlight menu choices as the user mouseovers. Plan to break up the banner graphic and us CSS \<div\> to position the different parts. Can you point me to this CSS hover effect you speak of? Does it work in all current and recent browsers?

Here is the webpage so you can see the banner I am trying to split up:

[http://www.silviasorganicfarm.com](http://www.silviasorganicfarm.com)

HOME ABOUT PRODUCE and CONTACT are the menu choices, HOME is highlighted like I want to do this.

Thanks

---

<div class="post-metadata">

### Author: ![GuanoLad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/guanolad/32/42_2.png) [@GuanoLad](https://boards.straightdope.com/u/GuanoLad)
#### Post date: [May 5, 2011, 4:46am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/4 "2011-05-05T04:46:57Z")

</div>

Use the CSS :hover effect to change the background. Something like this.

```auto

#menu div.button {
	width: 150px;
	height: 30px;
}

#menu div.button a {
	background: url(/images/buttonbg.gif);
	text-decoration: none;
}

#menu div.button a:hover {
	background: url(/images/buttonbghover.gif);
}

```

There are ways, using entirely CSS, to do everything you want, without needing image slices, by just applying opacity etc, but that’s complicated for a beginner.

---

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 5, 2011, 5:00am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/5 "2011-05-05T05:00:57Z")

</div>

That code would be inside a \<div\> tag of the image slice?

I have the stylesheet in a different file. Here is the messed up HTML:

> [@](#):
>
> \<html\>
> 
> \<head\>
> 
> \<link rel=“stylesheet”  
> type=“text/css” href=“ex1.css” /\>  
> \</head\>
> 
> \<body\>  
> \<div class=“banner”\>\<IMG SRC=“banner.jpg”\>\</div\>  
> \<div class=“picture”\>\<IMG SRC=“strawberry.png”\>\</div\>  
> \<div class=“topbox”\>\<p\>Welcome to Silvia’s Organic farm. I am Silvia Lucassians and I  
> grow the best organic vegatables in beautiful Salinas, California. Salinas is located along the central  
> California coast, south of San Francisco, and is blessed with a gentle cool climate and rich soil that  
> produces the most flavorful and nutritions organic produce.\<br\>\</br\>  
> I grow my food at ALBA, on certified organic land. No chemical pesticides or fertilizers are ever used.\<br\>\</br\>  
> All food is harvested at the peak of freshness and sent directly to the consumer.  
> \<a href=“[www.silviasorganicfarm.com/test.html](http://www.silviasorganicfarm.com/test.html)”  
> onmouseover=“button1.src=‘button1active.jpeg’”  
> onmouseout=“button1.src=‘button1inactive.jpeg’”\>  
> \<img name=“button1” width=75 height=75 border=0 src=“button1inactive.jpeg”\>\</a\>\</script\>  
> \</p\>\</div\>
> 
> \<div class=“links”\>  
> \<A HREF=“[http://www.facebook.com/profile.php?id=100001522928623](http://www.facebook.com/profile.php?id=100001522928623)” target=“\_blank”\>\<IMG SRC=“FacebookSmall.png” alt=“Facebook Page”\>\</A\>  
> \<A HREF=“[http://www.myspace.com/silvialucas](http://www.myspace.com/silvialucas)” target=“\_blank”\>\<IMG SRC=“myspaceSmall.png” alt=“Myspace Page”\>\</A\>  
> \</div\>
> 
> \</body\>
> 
> \</html\>

Here is the css file:

> [@](#):
>
> body  
> {  
> font-family: Arial, Helvetica, sans-serif;  
> background-color:#33aa33;  
> }
> 
> p  
> {  
> font-size:20px;  
> z-index: 50;  
> text-align:left;  
> color:#cccc22;  
> }
> 
> div.picture  
> {  
> z-index: 70;  
> position:absolute;  
> left:200px;  
> top:300px;  
> width:2000px;  
> padding:0px;  
> border:0px;  
> margin:0px;  
> }  
> div.banner  
> {  
> z-index: 90;  
> position:fixed;  
> left:0px;  
> top:0px;  
> width:2000px;  
> padding:0px;  
> border:0px;  
> margin:0px;  
> }
> 
> div.links  
> {  
> z-index: 50;  
> position:absolute;  
> left:300px;  
> width:70%;  
> top:800px;  
> width:520px;  
> }
> 
> div.topbox  
> {  
> background-color:#229922;  
> position:absolute;  
> left:100px;  
> top:700px;  
> width:620px;  
> padding:10px;  
> border:3px solid goldenrod;  
> margin:0px;  
> }  
> }

---

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 5, 2011, 5:07am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/6 "2011-05-05T05:07:22Z")

</div>

Or does the code go in the .css file for the div description.

/not exactly a beginner, just with javascript and css  
//worked with Radio Shack Level II Basic and a lot of other programming languages up to SQL

---

<div class="post-metadata">

### Author: ![GuanoLad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/guanolad/32/42_2.png) [@GuanoLad](https://boards.straightdope.com/u/GuanoLad)
#### Post date: [May 5, 2011, 5:11am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/7 "2011-05-05T05:11:31Z")

</div>

In the HTML, place inside the “div.banner” one div per menu item, each called “button.” Put the text of the links inside each div.

Then place my code in the CSS file.

It’s far from complete, it was just an example of how to use the “background” CSS option. There’d be a lot more required to make it work the way you want.

---

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 5, 2011, 5:15am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/8 "2011-05-05T05:15:28Z")

</div>

> [@GuanoLad](#):
>
> In the HTML, place inside the “div.banner” one div per menu item, each called “button.” Put the text of the links inside each div.
> 
> Then place my code in the CSS file.
> 
> It’s far from complete, it was just an example of how to use the “background” CSS option. There’d be a lot more required to make it work the way you want.

The thing is, I used to be able to do it so easily in javascript, but I am rusty and can’t make any of this stuff work.

I am all for CSS, and am trying to learn it. Perhaps you know an online tutorial that can step me through this. I am doing this site for a friend, not getting anything monetary out of it. I just want to help her promote her farming.

---

<div class="post-metadata">

### Author: ![GuanoLad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/guanolad/32/42_2.png) [@GuanoLad](https://boards.straightdope.com/u/GuanoLad)
#### Post date: [May 5, 2011, 5:23am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/9 "2011-05-05T05:23:53Z")

</div>

Let’s take this offsite, we can get through this via email because it would take half a day for me to go through the best way to achieve all you want.

I’ve sent you a PM.

---

<div class="post-metadata">

### Author: ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)
#### Post date: [May 5, 2011, 3:04pm UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/10 "2011-05-05T15:04:35Z")

</div>

Quick version…

For each “button”:

```auto

<img src="button.gif" onMouseOver="this.src= 'button_over.gif';" onMouseOut="this.src='button.gif';" border="0" />

```

---

<div class="post-metadata">

### Author: ![GuanoLad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/guanolad/32/42_2.png) [@GuanoLad](https://boards.straightdope.com/u/GuanoLad)
#### Post date: [May 6, 2011, 5:20am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/11 "2011-05-06T05:20:08Z")

</div>

[Here’s my example](http://guanolad.com/websites/organic/) of how to use CSS and png transparency to get a rollover effect on top of another image.

There are many different, and arguably better, methods.

---

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 6, 2011, 6:19am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/12 "2011-05-06T06:19:19Z")

</div>

> [@ZipperJJ](#):
>
> Quick version…
> 
> For each “button”:
> 
> ```auto
> 
> <img src="button.gif" onMouseOver="this.src= 'button_over.gif';" onMouseOut="this.src='button.gif';" border="0" />
> 
> ```

This seems to work. But it doesn’t seem to be javascript. Some new part of HTML? Very simple. So far so good, but this looks like exactly what I want. If I can stick these in a \<div\> box and put them where I want, I am gold. Thanks.

---

<div class="post-metadata">

### Author: ![jjimm](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jjimm](https://boards.straightdope.com/u/jjimm)
#### Post date: [May 6, 2011, 6:51am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/13 "2011-05-06T06:51:49Z")

</div>

Stan, I’ve written a JS routine that does what you want but, over and above the CSS option, it also pre-loads the alternative images when the page loads in. Over slower connections, not preloading the images can cause a placeholder on hover for a few seconds, rather than the alternative image. PM me if you’d like me to send it.

---

<div class="post-metadata">

### Author: ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)
#### Post date: [May 6, 2011, 3:01pm UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/14 "2011-05-06T15:01:51Z")

</div>

> [@Stan\_Shmenge](#):
>
> This seems to work. But it doesn’t seem to be javascript. Some new part of HTML? Very simple. So far so good, but this looks like exactly what I want. If I can stick these in a \<div\> box and put them where I want, I am gold. Thanks.

No, it’s totally Javascript. It’s super basic Javascript. The most simplest way of doing a mouseover. “When the mouse is on this object, the src value of the object is button\_over.gif. When the mouse is not on this object, the src value of the object is button.gif.”

What you really should do is make a table and put your entire header in it and be done with all of this nonsense. No divs, no transparent pngs, no weird shit…

```auto

<table cellpadding="0" cellspacing="0" border="0">
<tr><td colspan="5"><img src="top_part.gif"/></td></tr>
<tr><td><img src="button1.gif"/></td>
<td><img src="button2.gif"/></td>
<td><img src="button3.gif"/></td>
<td><img src="button4.gif"/></td>
<td><img src="the_rest.gif"/></td></tr>
</table>

```

The way to make that work is to take the entire header and slice it in to 6 parts - a top part, 4 buttons and the picture to the right of the button. Then put it back together with the above table coding.

If you set your body margin & padding to 0, you can set that whole table on top of a background that is the same as the background of the header, except super wide. That way your header can be static sized and your background can be chopped off by the browser screen thus eliminating the side scroll.

I’ve said too much. Hope this doesn’t confuse you more…

---

<div class="post-metadata">

### Author: ![BigT](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bigt/32/12044_2.png) [@BigT](https://boards.straightdope.com/u/BigT)
#### Post date: [May 6, 2011, 5:57pm UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/15 "2011-05-06T17:57:51Z")

</div>

I’m glad to see that the idea that HTML tables are evil is fading away again.

---

<div class="post-metadata">

### Author: ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)
#### Post date: [May 6, 2011, 6:53pm UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/16 "2011-05-06T18:53:05Z")

</div>

Well, this thread is 10+ posts about building shit in CSS and me, a 15-year-veteran suggesting tables, so don’t be so quick to assume the meme has left us.

Unless this happens to go 24 hours without an argument to my post, then yes, we may claim victory.

---

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 12, 2011, 2:32am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/17 "2011-05-12T02:32:45Z")

</div>

> [@ZipperJJ](#):
>
> Well, this thread is 10+ posts about building shit in CSS and me, a 15-year-veteran suggesting tables, so don’t be so quick to assume the meme has left us.
> 
> Unless this happens to go 24 hours without an argument to my post, then yes, we may claim victory.

Well, I got it to work with \<div\> and some simple code, but the whole thing breaks down in IE. Works great in Firefox and Chrome, so I think your table method may be the best way for good cross browser operability. God I hate Explorer.

And I am developing on XP so I can’t even install IE9. So here’s a big fark you to Microsoft. You used to be cool. Sometime around Word 2.0 you were really hitting your stride! What happened to THAT Microsoft? The Microsoft that could make the competition a memory by coming up with such a cool product? The wreath was layed upon that grave a long time ago…

---

<div class="post-metadata">

### Author: ![Stan\_Shmenge](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Stan\_Shmenge](https://boards.straightdope.com/u/Stan_Shmenge)
#### Post date: [May 12, 2011, 2:33am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/18 "2011-05-12T02:33:52Z")

</div>

And here’s a link to the page in question-

www.silviasorganicfarm.com

---

<div class="post-metadata">

### Author: ![Hambil](https://avatars.discourse-cdn.com/v4/letter/h/3be4f8/32.png) [@Hambil](https://boards.straightdope.com/u/Hambil)
#### Post date: [May 12, 2011, 2:42am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/19 "2011-05-12T02:42:52Z")

</div>

Most front end web development today seems to focus around YUI or jQuery. In general, it’s easier to use a library that provides the functions you need (and either of the ones mentioned above do just that) than to roll your own.

With that said, I have been out of the web development world for a couple years, so there may be a change in approach of late. But, a pre-existing library, especially for beginners, is often a big time saver since all the cross browser issues and other crap is already handled for you.

Here is an example of a jQuery image menu: [http://www.alohatechsupport.net/examples/image-menu-1.html](http://www.alohatechsupport.net/examples/image-menu-1.html)

---

<div class="post-metadata">

### Author: ![Duckster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/duckster/32/1244_2.png) [@Duckster](https://boards.straightdope.com/u/Duckster)
#### Post date: [May 12, 2011, 3:15am UTC](https://boards.straightdope.com/t/arrgh-need-simple-tutorial-for-javascript-mouseover-for-changing-image/580807/20 "2011-05-12T03:15:30Z")

</div>

> [@Hambil](#):
>
> Most front end web development today seems to focus around YUI or jQuery.

This.

[Try this one](http://roshanbh.com.np/2008/03/image-hover-effect-jquery.html).
