473,505 Members | 16,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mouseover change div and image

Dear all,
I am a very novice javascriper and have cobled this together from
various places. I didnt expect it to work but hopefully will give you
an idea of what i want to achive.
I need 5 links that when you mouseover it, it displays the corisponding
div and separate image. When you mouse out it goes back to the defult.
first link.
I hope you get the idea.
Please help as i will never mange this on my own
Many thanks
Alex Kemsley
<html>
<head>
<title></title>

<script type="text/javascript">
function doStuff(el) {
document.all.tubimage.src=el+".jpg";
document.getElementById.el.style.display="block";

el.onmouseout=function() {
document.all.tubimage.src="monaco.jpg";
document.getElementById.el.style.display="none";
}
}

</script>

</head>
<body>
<p>
<img src="monaco.jpg" alt="" id="tubimage" /></p>

<a onmouseover="doStuff(monaco)">Monaco</a><br />
<a onmouseover="doStuff(sttropez)">St Tropez</a<br />
<a onmouseover="doStuff(barclona)"Barcelona</a><br />
<a onmouseover="doStuff(milan)">Milan</a><br />
<a onmouseover="doStuff(prague)">Prague</a><br />
</p>
<div id="monaco" style="display:none">
monaco text
</div>
<div id="sttropez" style="display:none">
sttropez text
</div>
<div id="barcelona" style="display:none">
barclona text
</div>
<div id="milan" style="display:none">
milan text
</div>
<div id="prague" style="display:none">
prague text
</div>

</body>
</html>

Jul 26 '06 #1
4 3175
First, quote the values in the links.
change this:
<a onmouseover="doStuff(monaco)">Monaco</a><br />
to this:
<a onmouseover="doStuff('monaco')">Monaco</a><br />

Next, use getElementById as a method:
document.getElementById.el.style.display="block";
becomes:
document.getElementById(el).style.display="block";

Your el.mouseout is completely broken but, with good intentions. delete
that.

I'm going to recommend that you do the following:
make your links like this:
<a onmouseover="show('monaco')"
onmouseout="hide('monaco')">Monaco</a><br />
and use some js like this:
function show(name){
document.all.tubimage.src=name+".jpg";
document.getElementById(name).style.display="block ";
}
function hide(name){
document.all.tubimage.src="monaco.jpg";
document.getElementById(name).style.display="none" ;
}

There are some better ways to do this but, it looks like you are still
getting started.

Jul 26 '06 #2
Paul Davis said the following on 7/26/2006 2:35 PM:
First, quote the values in the links.
No, first you quote what you are replying to.
change this:
<a onmouseover="doStuff(monaco)">Monaco</a><br />
to this:
<a onmouseover="doStuff('monaco')">Monaco</a><br />
OK, not sure I agree but ok.
Next, use getElementById as a method:
There is no other way to use it.....
document.getElementById.el.style.display="block";
becomes:
document.getElementById(el).style.display="block";

Your el.mouseout is completely broken but, with good intentions. delete
that.
I'm going to recommend that you do the following:
Don't.
make your links like this:
<a onmouseover="show('monaco')"
onmouseout="hide('monaco')">Monaco</a><br />
and use some js like this:
function show(name){
document.all.tubimage.src=name+".jpg";
And then wonder why it only works in IE?

Besides, don't use document.all or gEBI to access an image, use the
Images collection:

document.images['tubimage'].src = name + ".jpg";
document.getElementById(name).style.display="block ";
}
function hide(name){
document.all.tubimage.src="monaco.jpg";
Ditto.
document.getElementById(name).style.display="none" ;
}

There are some better ways to do this but,
There are more better ways to do it than worse ways to do it than the
way you did it.
it looks like you are still getting started.
Then now is the time to teach good habits, not to say "You are just
getting started so here are some bad habits for you.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 26 '06 #3
~A!

Randy Webb wrote:
Paul Davis said the following on 7/26/2006 2:35 PM:
First, quote the values in the links.

No, first you quote what you are replying to.
change this:
<a onmouseover="doStuff(monaco)">Monaco</a><br />
to this:
<a onmouseover="doStuff('monaco')">Monaco</a><br />

OK, not sure I agree but ok.
Next, use getElementById as a method:

There is no other way to use it.....
document.getElementById.el.style.display="block";
becomes:
document.getElementById(el).style.display="block";

Your el.mouseout is completely broken but, with good intentions. delete
that.
I'm going to recommend that you do the following:

Don't.
make your links like this:
<a onmouseover="show('monaco')"
onmouseout="hide('monaco')">Monaco</a><br />
and use some js like this:
function show(name){
document.all.tubimage.src=name+".jpg";

And then wonder why it only works in IE?

Besides, don't use document.all or gEBI to access an image, use the
Images collection:

document.images['tubimage'].src = name + ".jpg";
document.getElementById(name).style.display="block ";
}
function hide(name){
document.all.tubimage.src="monaco.jpg";

Ditto.
document.getElementById(name).style.display="none" ;
}

There are some better ways to do this but,

There are more better ways to do it than worse ways to do it than the
way you did it.
it looks like you are still getting started.

Then now is the time to teach good habits, not to say "You are just
getting started so here are some bad habits for you.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Well, that was nasty for no good reason. Nice ta meetcha.

~A!

Jul 27 '06 #4
Since I am a bad person:

var toggleDivs = (function() {
return {
show : function(anchorRef,elemId) {
document.getElementById("tubimage").src= elemId+".jpg";
document.getElementById(elemId).style.display = "block";
anchorRef["elemId"] = elemId;
anchorRef.onmouseout = toggleDivs.hide;

},
hide : function() {
document.getElementById("tubimage").src = "monaco.jpg";
document.getElementById(this["elemId"]).style.display = "none";
}
};
})();
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
<a onmouseover="toggleDivs.show(this,'monaco')" href="#">Monaco</a><br
/>
<a onmouseover="toggleDivs.show(this,'sttropez')" href="#">St
Tropez</a><br />
<a onmouseover="toggleDivs.show(this,'barcelona')"
href="#">Barcelona</a><br />
<a onmouseover="toggleDivs.show(this,'milan')" href="#">Milan</a><br />
<a onmouseover="toggleDivs.show(this,'prague')" href="#">Prague</a><br
/>

Jul 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
78164
by: al | last post by:
Hi, I've been trying to create a image mouseOver effect using CSS - is this possible? Or will I have to use DIV tags or something along those lines? I've tried a number of things - the code...
3
13449
by: John | last post by:
Imagine this: I have a gif-image with text, coloured green. When MouseOver occurs the gif will swap to another gif with red colourded text. On MouseOut the gif turns back to the green text. ...
3
1758
by: richk | last post by:
For some reason when I add additional buttons a 3rd button and beyond i cant get the effect to work and I get errors...I cant understand why... <SCRIPT LANGUAGE = "javascript"><!-- if...
14
2316
by: J. Makela | last post by:
Hallo. This should be a pretty entertaining question for you *real* javascript writers.. I, being the lowly photoshop guy at an ad agency made the mistake of actually saying "HTML" in a...
6
14673
by: Selden McCabe | last post by:
I have a form with a bunch of image buttons. When the user moves the mouse over a button, I want to do two things: 1. change the Imagebutton's picture, and 2. make another control visible. I'm...
2
17712
by: Bendik Engebretsen | last post by:
I'm an ASP newbie and have just started experimenting with ASP.NET 2.0 using the VS.NET 2005 Beta. As a starting point I have used the 'Personal WEB site' starter kit. I am now trying to figure...
2
3999
by: Amy | last post by:
<style> div.helpBtn{ font:bold 73% verdana; color:#995F8D; text-align:left; width:79px; height:24px; margin:0px; padding: 5px 0px 0px 27px; background:...
3
19490
by: Annette Acquaire | last post by:
I have and image map with a dozen hotspot links on it that I'm trying to get to open a new image over existing one on mouseover of each COORD. The only thing I was able to do was swap image on...
1
1942
by: dave345 | last post by:
This javascript issue is in an app using C# / .Net 2.0 running on XP. First post, please mention if I mess up any conventions of this forum. I’ve got a mouseover event that only works properly...
0
1704
by: spacecadet563 | last post by:
I am trying to change and display the image source of an image control (named image2 )on mouseover of cell event on a gridview(Gridview1) with a SQL datasource. The cell contains image name of image...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.