Sanjeev K Sanjeev K - 3 months ago 17
HTML Question

Change image url on div hover instead image hover

I am trying to change the url of image on mouseover, its working fine but what I am trying is, hover on div instead of image and change the image url

following the

jQuery
I have:

$(".logo > img").on({
"mouseover" : function() {
this.src = 'images/logo-white.png';
},
"mouseout" : function() {
this.src='images/logo-black.png';
}


HTML

<div class="logo">
<img src="images/logo-white.png">
</div>


How do I make it on div hover? please help!

Answer

If this is the HTML

<div class="logo">
   <img src="images/logo-white.png">
</div>

then this will work:

$(".logo").on({
  "mouseover": function() {
    $(this).find("img").attr("src", "images/logo-white.png");
  },
  "mouseout": function() {
    $(this).find("img").attr("src", "images/logo-black.png");
  }
});

Alternative: .hover - which is using less }

$(".logo").hover(
  function() {$(this).find("img").attr("src", "images/logo-white.png");},
  function() {$(this).find("img").attr("src", "images/logo-black.png");}
);