Join 118,308 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,686 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
Hey everyone ! I am not very advanced in programing and am having trouble creating a javascript function inside of an Html page. I have two images side by side, and when there is a mouseover on one of the images i want the image to switch to a different image and want new text to be displayed underneath. It took me about 12 hours to write the code below and it works to switch the image with another one on mouse over but i can't get the text to work.
so i'm trying to make the function display the text at the bottom of the page (<p>) when mouseover and display no text when Mouse out (for 2 different images)
hello there you can put all you images pathes in array and the corresponing text in another array on mouse over for each image call a function and pass it the index of the image you want to show, use the same index to show the text
jscript
<html>
<head> <title>Untitled 1</title> <script> imageOverArr=new Array("b1.gif","b2.gif"); imageOutArr=new Array("c1.gif","c2.gif"); textArr=new Array("hello your mouse is over b1","hello your mouse is over b2"); function mouseOver(obj,num){ obj.src=imageOverArr[num]; document.getElementById("myText").innerHTML=textArr[num]; } function mouseOut(obj,num){ obj.src=imageOutArr[num] document.getElementById("myText").innerHTML=""; }
ahmad_511, your post was really helpful for a project I'm working on. I'm pretty new at this, too.
Is there a way to use this function and have a line of text present in that same spot when there is no mouseover, and have it switch when you mouseover one of the images?
In the example below I want the cell where the onmousover text displays to say "E-ship Program Description" when there is no event, but switch to the Engineering Management Program Description or Deming Center Program Description with a mouseover on the images.
<script> imageOverArr=new Array("EMP_but2.gif","Deming_but2.gif"); imageOutArr=new Array("EMP_but1.gif","Deming_but1.gif"); textArr=new Array("Engineering Management Program Description","Deming Center Program Description"); function mouseOver(obj,num){ obj.src=imageOverArr[num]; document.getElementById("myText").innerHTML=textArr[num]; } function mouseOut(obj,num){ obj.src=imageOutArr[num] document.getElementById("myText").innerHTML=""; }
hey... thanks for this.. its exactly what I've been working on now for 3 days... one thing though... I have 18 pictures and it stops working after 9... is there some extra code I can put into this to make it work?
Hello, can you post the code you're already working on, so we can discuss it here? Regards
yes of course... the first one is the code when its working using only 9 pictures and the second one exactly the same just using 18 pictures and the text doesn't work anymore but the swapping pictures does:
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>
image1= new Image(100,100); image1.src="images/cast/01.gif"; image2= new Image(100,100); image2.src="images/cast/02.gif"; image3= new Image(100,100); image3.src="images/cast/03.gif"; image4= new Image(100,100); image4.src="images/cast/04.gif"; image5= new Image(100,100); image5.src="images/cast/05.gif"; image6= new Image(100,100); image6.src="images/cast/01.gif"; image7= new Image(100,100); image7.src="images/cast/07.gif"; image8= new Image(100,100); image8.src="images/cast/08.gif"; image9= new Image(100,100); image9.src="images/cast/09.gif";
}
function change1(picName,imgName){
if (document.images) { imgOn=eval(imgName + ".src"); document[picName].src= imgOn; } }
textArr=new Array("hello your mouse is over b1", "hello your mouse is over b2", "hello your mouse is over b3", "hello your mouse is over b4", "hello your mouse is over b5", "hello your mouse is over b6", "hello your mouse is over b7", "hello your mouse is over b8", "hello your mouse is over b9");
function mouseOver(obj,num){ obj.src=imageOverArr[num]; document.getElementById("myText").innerHTML=textArr[num]; }
</script>
<!-- End Preload Script -->
</head>
<!-- BODY BEGINS --> <body>
<div id="container">
<!-- MAIN CONTENT -->
<div id="area2crew"> <p>Place the mouse over the icon in front of each name to see more detail.<br /><br />
image1= new Image(100,100); image1.src="images/cast/01.gif"; image2= new Image(100,100); image2.src="images/cast/02.gif"; image3= new Image(100,100); image3.src="images/cast/03.gif"; image4= new Image(100,100); image4.src="images/cast/04.gif"; image5= new Image(100,100); image5.src="images/cast/05.gif"; image6= new Image(100,100); image6.src="images/crew/06.gif"; image7= new Image(100,100); image7.src="images/cast/07.gif"; image8= new Image(100,100); image8.src="images/cast/08.gif"; image9= new Image(100,100); image9.src="images/crew/09.gif"; image10= new Image(100,100); image10.src="images/cast/01.gif"; image11= new Image(100,100); image11.src="images/crew/09.gif"; image12= new Image(100,100); image12.src="images/cast/02.gif"; image13= new Image(100,100); image13.src="images/cast/03.gif"; image14= new Image(100,100); image14.src="images/cast/04.gif"; image15= new Image(100,100); image15.src="images/cast/05.gif"; image16= new Image(100,100); image16.src="images/crew/06.gif"; image17= new Image(100,100); image17.src="images/cast/07.gif"; image18= new Image(100,100); image18.src="images/cast/08.gif";
}
function change1(picName,imgName){
if (document.images) { imgOn=eval(imgName + ".src"); document[picName].src= imgOn; } }
textArr=new Array("hello your mouse is over b1", "hello your mouse is over b2", "hello your mouse is over b3", "hello your mouse is over b4", "hello your mouse is over b5", "hello your mouse is over b6", "hello your mouse is over b7", "hello your mouse is over b8", "hello your mouse is over b9", "hello your mouse is over b10", "hello your mouse is over b11", "hello your mouse is over b12", "hello your mouse is over b13", "hello your mouse is over b14", "hello your mouse is over b15", "hello your mouse is over b16", "hello your mouse is over b17", "hello your mouse is over b18");
function mouseOver(obj,num){ obj.src=imageOverArr[num]; document.getElementById("myText").innerHTML=textArr[num]; }
</script>
<!-- End Preload Script -->
</head>
<!-- BODY BEGINS --> <body onLoad="preloadImages();">
<a href="#" onMouseover="change1('pic1','image6')" onMouseout="change1('pic1','image_off')"> <img src="images/layout/dott.gif" onmouseover="mouseOver(this,05)" onmouseout="mouseOut(this,0)"></a> Camera Operator: Elva Sara Ingvarsdóttir<br /><br />
<a href="#" onMouseover="change1('pic1','image16')" onMouseout="change1('pic1','image_off')"> <img src="images/layout/dott.gif" onmouseover="mouseOver(this,15)" onmouseout="mouseOut(this,0)"></a> Camera Operator: Elva Sara Ingvarsdóttir<br /><br />