Skip to content Skip to sidebar Skip to footer

Change Image Onmouseover

What's the correct way to change an image on mouseover and back on mouseout (with/without jQuery)?

Solution 1:

here's a native javascript inline code to change image onmouseover & onmouseout:

<ahref="#"id="name"><imgtitle="Hello"src="/ico/view.png"onmouseover="this.src='/ico/view.hover.png'"onmouseout="this.src='/ico/view.png'" /></a>

Solution 2:

Try something like this:

HTML:

<img src='/folder/image1.jpg'id='imageid'/>

jQuery:

$('#imageid').hover(function() {
  $(this).attr('src', '/folder/image2.jpg');
}, function() {
  $(this).attr('src', '/folder/image1.jpg');
});

EDIT:(After OP HTML posted)

HTML:

<ahref="#"id="name"><imgtitle="Hello"src="/ico/view.png"/></a>

jQuery:

$('#name img').hover(function() {
  $(this).attr('src', '/ico/view1.png');
}, function() {
  $(this).attr('src', '/ico/view.png');
});

Solution 3:

Thy to put a dot or two before the /

('src','./ico/view.hover.png')"

Solution 4:

Here is an example:

HTML code:

<img id="myImg" src="http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif"/>

JavaScript code:

$(document).ready(function() {
    $( "#myImg" ).mouseover(function(){
        $(this).attr("src", "http://www.jqueryui.com/images/logo.gif");
    });

    $( "#myImg" ).mouseout(function(){
        $(this).attr("src", "http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif");
    });
});

Edit: Sorry, your code was a bit strange. Now I understood what you were doing. ;) The hover method is better, of course.

Solution 5:

jQuery has .mouseover() and .html(). You can tie the mouseover event to a function:

  1. Hides the current image.
  2. Replaces the current html image with the one you want to toggle.
  3. Shows the div that you hid.

The same thing can be done when you get the mouseover event indicating that the cursor is no longer hanging over the div.

Post a Comment for "Change Image Onmouseover"