Sanjeev Kumar
Sanjeev Kumar

Reputation: 3163

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!

Upvotes: 3

Views: 100

Answers (3)

mplungjan
mplungjan

Reputation: 178422

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");}
);

Upvotes: 5

Lalji Tadhani
Lalji Tadhani

Reputation: 14179

try this

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

https://jsfiddle.net/nkugogop/2/

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 5244

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

Upvotes: 0

Related Questions