473,666 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make an image change when mouse over and then change back when mouse out?

10 New Member
As the title says i'd like to have a image (mocha1150.Gif) and when the mouse moves over them id like them to change to mocha5150ani.Gi f and when the mouse moves out id like to change it back to mocha1150.Gif

I already have the code written can someone please tell me whats wrong with it?

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function moveover(imga,ida)
  5. {
  6. document.getElementById('ida').src="imga";
  7. }
  8. function moveout(imgb,idb)
  9. {
  10. document.getElementById('idb').src="imgb";
  11. }
  12. </script>
  13. </head>
  14.  
  15.  
  16.  
  17.  
  18. <div style="position:absolute;top:20px;left:20%" align="center"><font face="arial" id="title" size="20" >Welcome to Spencer's Test website</font></div>
  19. <img src="top.GIF" width="100%" height="75px" />
  20. <br>
  21.  
  22.  
  23.  
  24. <img id="aa" src="mocha1150.GIF"
  25. onmouseover="moveover(mocha5150ani.GIF,aa)"
  26. onmouseout="moveout(mocha1150.GIF,aa)"  />
  27.  
  28.  
  29. <html>
  30.  
  31.  
also if theirs a way to do it without creating a function and doing it within the element can someone please tell me how?
Jan 23 '11 #1
4 5969
Dormilich
8,658 Recognized Expert Moderator Expert
first, function make coding easier. no need to drop them.

second, in JavaScript variables are not quoted. i.e. in your functions you define 2 parameters, but you don’t use them. in contrast to that, strings must always be quoted.

third, you have 2 functions doing exactly the same …

fourth, (advanced) you don’t need the document.getEle mentById() call. using the keyword for the current element is cleaner and shorter.
Expand|Select|Wrap|Line Numbers
  1. function changeSource(pic, obj)
  2. {
  3.     obj.src = pic;
  4. }
Expand|Select|Wrap|Line Numbers
  1. <img src="mocha1150.GIF" onmouseover="changeSource('mocha5150ani.GIF', this)" onmouseout="changeSource('mocha1150.GIF', this)"  />
PS. even that can be simplified, but that’s for later.
Jan 23 '11 #2
Spencer Killen
10 New Member
0k I updated my code, but it still does'nt work it displays the mouseout image only
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function change(img,id)
  5. {
  6. id.src = img;
  7. }
  8.  
  9. </script>
  10. </head>
  11.  
  12. <div style="position:absolute;top:20px;left:20%" align="center"><font face="arial" id="title" size="20" >Welcome to Spencer's Test website</font></div>
  13. <img src="top.GIF" width="100%" height="75px" />
  14. <br>
  15.  
  16. <img src="mocha1150.GIF"
  17. onmouseover="change(mocha5150ani.GIF,this)"
  18. onmouseout="change(mocha1150.GIF,this)"  />
  19.  
  20. <img src="mocha2150.GIF"
  21. onmouseover="change(mocha5150ani.GIF,this)"
  22. onmouseout="change(mocha2150.GIF,this)"  />
  23.  
  24. <img src="mocha3150.GIF"
  25. onmouseover="change(mocha5150ani.GIF,this)"
  26. onmouseout="change(mocha3150.GIF,this)"  />
  27.  
  28. <img src="mocha4150.GIF"
  29. onmouseover="change(mocha5150ani.GIF,this)"
  30. onmouseout="change(mocha4150.GIF,this)"  />
  31. <html>
  32.  
Jan 23 '11 #3
drhowarddrfine
7,435 Recognized Expert Expert
Is there a reason why you are using javascript to do this when CSS can far easier?
Jan 23 '11 #4
Bharat383
93 New Member
using css ::

Expand|Select|Wrap|Line Numbers
  1. #div
  2. {
  3.  background:orange;
  4. }
  5. #div:hover
  6. {
  7.  background:url('images/img1.jpg');
  8. }
Bharat Parmar(Bharat38 3)
Apr 21 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1489
by: Daniel | last post by:
how to change the value of Request.ServerVariables on the ser ver? if not possible is there some way to change it by sending back javascript to the client and having the client automaticaly do something to change the value of Request.ServerVariables I would like to reset the value in Request.ServerVariables
1
14699
by: Sakharam Phapale | last post by:
Hi All, How to capture Mouse Single click and mouse double click event on Commnad Button. I am doing as follows. Private void Button1_MouseUp(Object sender, System.Windows.Forms.MouseEventArgs e) {
2
1445
by: cyshao | last post by:
I opened an image and want to change its size, then display it in a ImageBox. How can I do that ? Thanks Charles Shao
6
3737
by: guoqi zheng | last post by:
In a regular html form, when user press "enter" key, the form will be submitted. However, in ASP.NET web form, a form will only be submitted (post back) when a special button is clicked. Many user get used to click "enter" key to submit a form. How can I use "enter" key to submit/postback in ASP.NET. Thank, Guoqi Zheng
5
1888
by: Mr Newbie | last post by:
OK, I have a fairly simple setup where I have an Index of records which are displayed using a datagrid on a page. When you click the link associated with a Row (ArticleID) , this ID is passed to a page which creates an new Article or Retreive an existing one depending if there is an ArticleID passed to it. So on my index page the user will be directed to the article page and will either pick up an existing one or have a new one created....
3
7423
by: ramkishor | last post by:
How can I change the color of button on mouse move over it. help .
3
1819
by: Marc | last post by:
Anyone know how to add a drop down menu to a button that will appear when you right click the mouse?
0
1748
by: williamr4j | last post by:
Very first thing, after preloader, is a blurry image that is supposed to get clear when mouse goes over the image. Problem is, the image that is clear will not, when previewing the movie, move from the top left corner. It is supposed to be right on top of the other image, which is in the middle of the screen. when i go into the layer that the clear image is it is right over the blurry image. I don't understand why, and have tried many different...
0
1769
by: mykeaster | last post by:
In .NET Forms you could Dock a control to an edge of it's parent container. However there does not seem to be anything like this in WPF. Right now I am writing an app that needs to run Full-Screen, but the main Image I am trying to use is not resizing with the rest of the window when I put it in Full-Screen Mode. This Image is incredibly large (2500x1900) so when I set the Height and Width to AUTO the Image blows up. Any ideas or suggestions??
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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 we have to send another system
2
1776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.