Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change image onmouseover

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

<a href="#" id="name">
    <img title="Hello" src="/ico/view.png" onmouseover="$(this).attr('src','/ico/view.hover.png')" />
</a>

Ok, this is working, but how to change back to the original image after mouseout?

If it is possible, I want to do this thing inline, without document.ready function.

like image 314
Adrian Avatar asked May 22 '12 19:05

Adrian


People also ask

How do I change the image on my mouse hover?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

What does onmouseover do in HTML?

The onmouseover attribute fires when the mouse pointer moves over an element. Tip: The onmouseover attribute is often used together with the onmouseout attribute.

What is onmouseover and Onmouseout?

Definition and UsageThe onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.

What is mouse over image?

A mouseover is an event that occurs in a Graphical User Interface (GUI) when the mouse pointer is moved over an object on the screen such as an icon, a button, text box, or even the edge of a window.


2 Answers

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

<a href="#" id="name">
    <img title="Hello" src="/ico/view.png" onmouseover="this.src='/ico/view.hover.png'" onmouseout="this.src='/ico/view.png'" />
</a>
like image 132
Dominic Avatar answered Sep 20 '22 20:09

Dominic


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');
});
  • DEMO

EDIT: (After OP HTML posted)

HTML:

<a href="#" id="name">
    <img title="Hello" src="/ico/view.png"/>
</a>

jQuery:

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

like image 31
Naveed Avatar answered Sep 22 '22 20:09

Naveed