3

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!

2
  • Do you have any unique class or id for image? Commented Aug 22, 2016 at 9:33
  • No unique class, image is just placed within a div Commented Aug 22, 2016 at 9:35

3 Answers 3

5

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");}
);
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

incorrect jQuery. A jQuery object does not have a .src
0

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/

1 Comment

That still only hovers the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.