Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change image in mouse over event using classic asp and javascript????
Posted
Comments
hypermellow 18-Sep-14 7:07am    
Hi, can you post the code of what you have tried so far?
Thanks

http://www.w3schools.com/jsref/event_onmouseover.asp[^]
Use this event and inside the function for this event toggle the source attribute of the image tagfor the image you want.
Hope this helps.
Thanks :)
 
Share this answer
 
If this is a fixed couple of images (they better should be), it's the best to use CSS for holding background image property with for two different classes: one for mouse over, another one for mouse out. In jQuery, change the style using the function .toggleClass. The mouse events you need is best to handle using .hover.

Your top-level script in the <body> element can look like this:
JavaScript
$( document ).ready(function() {
   myElement = $("..."); // use some selector to get jQuery wrapper for your element
   clsIn = //... CSS class name when mouse is in
   clsOut = //... CSS class name when mouse is out
   myElement.hover(
      function() { // mouse goes in:
         myElement.toggleClass(clsOut, false);   
         myElement.toggleClass(clsIn, true);
      }, function() { // mouse goes out:
         myElement.toggleClass(clsIn, false);   
         myElement.toggleClass(clsOut, true);
      }
   );
});


Alternatively, you can change src in <img> element, or anything else.

Please see:
http://api.jquery.com/hover[^],
http://api.jquery.com/toggleClass[^].

See also: http://api.jquery.com/category/css/[^].

[EDIT]

I already posted this solution and immediately remembered that it could be done in pure CSS, without any Javascript. I added the alternative solution as Solution 3.

But this pure-CSS solution is only good for some simple as the one described ago, when all changes can be described in turn of style properties. For some more complex cases, the solution shown above should still be used. For example, hovering mouse could modify some inner HTML of the targeted element. The code sample shown above should give you the idea.
—SA
 
Share this answer
 
v5
After I published Solution 2, I found that if you merely want to change the class, you can implement it in pure CSS, without any Javascript.
This is explained here: http://kyleschaeffer.com/development/pure-css-image-hover[^].

Background (not background image :-):
This technique uses CSS pseudo-classes. In this case, the pseudo-class .hover. Please see: http://www.w3.org/wiki/CSS/Selectors#Pseudo-classes[^].

I won't remove Solution 1, will only edit it, because it can be good for something a bit more complex, when you actually need to change some content on mouse hover.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900