0

I use the next, in order to change the picture, when I hover the image:

<img id="image1" src="imimage.jpg" alt="facebook.com">

$('#image1').hover(function() {
    $('#image1').attr('src', 'imanewimage.jpg');
}, function() { 
    $('#image1').attr('src', 'imimage.jpg'); 
}); 

But something really weird happens: When I load the HTML, the imimage.jpg loaded. But when I hover it, the imimage.jpg doesn't load and only the alt attribute is shown.

What can be the reason?

tymeJV
  • 103,943
  • 14
  • 161
  • 157
Tom Avni
  • 109
  • 3
  • 9

2 Answers2

1

Your code above is correct, the alt tag is showing because the image most likely isn't in the proper location. Based on your code, the image should be in the same directory as your pages.

Test this with images from the internet (you said they are working), must be a file location issue.

tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • I don't think it file location issue, because as I said, I can see the image before I hover it. and the src is the same. – Tom Avni May 10 '13 at 14:17
0

You could simplify this by using css to change the image instead of javascript, which would make it more universally compatible and easier to code...

<div class='imageToHover'></div>

.imageToHover {
   /*you probably want some height/width stuff in here too*/
   background-image: url(imimage.jpg);
}
.imageToHover:hover {
   background-image: url(imanewimage.jpg);
}
Charles Bandes
  • 795
  • 8
  • 21