It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Changing image on link mouseover

Changing image on link mouseover

Postby kyle79 on Fri Jun 17, 2011 10:02 pm

Hi there,

I have a menu with 4 links and 4 images associated with them. By default, the image from link 1 is displayed on the page. I would like to change the image with its corresponding one, each time i mouseover one of the other three links.

I'm trying to make the following code work unsuccesfully, i might be missing something. Any help will be appreciated.

In the <head> section i have this:

<script type="text/javascript">

img1 = new Image();
img1.src = "images/party/party.jpg";

img2 = new Image();
img2.src = "images/party/icecream.jpg";

img3 = new Image();
img3.src = "images/party/juice.jpg";


img4 = new Image();
img4.src = "images/party/videogames.jpg";

function change(num){

document.images["linkpic"].src = "img" + num
}

</script>

</head>

In the <body> i have:

That's my default image
<table>
<tr>
<td width="100">
<img src="images/party/balloon1.jpg" name="linkpic">
</td>
<td width="260" valign="top" align="left">
<table>
<tr><td><a href="Party1.html" onmouseover="change('1')">Party and Fun</a></td></tr>
<tr><td><a href="Party2.html" onmouseover="change('2')">Icecream
</a></td></tr>
<tr><td><a href="Party3.html" onmouseover="change('3')">Juice </a></td></tr>
<tr><td><a href="Party4.html" onmouseover="change('4')">Video Games
</a></td></tr>
</table>
</tr>
</table>
</body>

I would like when i point to Icecream, the picture on the left to change to the Icecream picture and so on.
Any hints and help are appreciated!

Thanks!
kyle79
 
Posts: 1
Joined: Fri Jun 17, 2011 9:58 pm

Re: Changing image on link mouseover

Postby rajmv on Thu Jul 14, 2011 6:16 pm

In the <head> section i have this:

<script type="text/javascript">
function change(imgSrc){
document.getElementById('linkpic').src = '/images/party/' +imgSrc;
}
</script>
</head>

In the <body> i have:

That's my default image
<table>
<tr>
<td width="100">
<img id="linkpic" name="linkpic" src="images/party/balloon1.jpg">
</td>
<td width="260" valign="top" align="left">
<table>
<tr><td><a href="Party1.html" onmouseover="change('party.jpg')">Party and Fun</a></td></tr>
<tr><td><a href="Party2.html" onmouseover="change('icecream.jpg')">Icecream
</a></td></tr>
<tr><td><a href="Party3.html" onmouseover="change('juice.jpg')">Juice </a></td></tr>
<tr><td><a href="Party4.html" onmouseover="change('videogames.jpg')">Video Games
</a></td></tr>
</table>
</tr>
</table>
</body>
rajmv
100+ Club
 
Posts: 103
Joined: Thu Jul 14, 2011 7:22 am


Who is online

Users browsing this forum: No registered users and 5 guests