Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to change an image when hoovering on the image. I am trying to test the outcome with jsFiddle but it is not working.

http://jsfiddle.net/8XKLj/[^]

Any ideas why?
Posted
Comments
OriginalGriff 6-Jul-14 11:56am    
It's not a dirty picture?
(I assume you mean "hovering", rather than "hoovering"... :laugh: )
Mohibur Rashid 6-Jul-14 21:01pm    
it works on local machine. but not on fiddle

This is because you did not even try to implement it. You added 3 independent mechanisms and did not use any of them:
  1. You included jQuery but never ever used a single function from this library.
  2. You added really working CSS with the style defined for the element <div>, but never ever added any of the this element to you HTML. Even if you applied this mechanism for <img> element, it would not work, because you would have too image, one in the background (changing) and another in foreground… making background hidden behind, so whole thing would not work.
  3. You added events to the element <img> to your HTML but never implemented the events properly; note that doing it is pointless if you are using jQuery which does it all without touching HTML in few lines of code.


As the CSS approach is more than enough, I'll show just this solution. As you probably would not want to apply this style to all divs, I added a class to just one instance of this element, called it "box":

CSS:
CSS
div.box {
    width: 100px; height: 100px;
    background: url('http://dummyimage.com/100x100/000/fff');
}
div.box:hover {
    width: 100px; height: 100px;
    background: url('http://dummyimage.com/100x100/eb00eb/fff');
}

HTML:
HTML
<div class="box" />


Another approach would be using jQuery: http://api.jquery.com/hover[^].

You need to use just one method not two or three at a time, as they could interfere with each other.

—SA
 
Share this answer
 
Comments
DamithSL 6-Jul-14 13:28pm    
correct, 5wd!
check http://jsfiddle.net/damithsw/8XKLj/6/[^]
under framework and extensions select no wrap - in <head></head> or no wrap - in <body></body> option
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Jul-14 13:22pm    
Yes, this is the fix. I voted 4 this time, just because: 1) the jQuery is available anyway but not used, which would be even simpler; 2) CSS solution is even simpler and would be more than enough.

Cheers,
—SA

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