-1

Total noob to jquery, i want to add another function on the cotsImg.hover event, to change a second image - just like it changes one below. something like

<script type='text/javascript'>
$(document).ready(function(){
 $("#cotsImg").hover(
 function() {$('#golfImg').attr("src","/images/golfimagebw.png");}, 
 function() {$('#golfImg').attr("src","/images/golfimage.png");
 function() {$('#golfImg2').attr("src","/images/golfimage2bw.png");}, 
 function() {$('#golfImg2').attr("src","/images/golfimage2.png");
});
});
</script>

Can anyone help? Its been a long day of trying this!

asked Aug 27, 2013 at 20:39
3
  • Could you show us the HTML? Commented Aug 27, 2013 at 20:41
  • You want to replace with new image on each hover? Explain better please Commented Aug 27, 2013 at 20:42
  • You should have 2 functions the first one for mouseenter the second one for mouseleave Commented Aug 27, 2013 at 20:58

3 Answers 3

2

You need to pass two functions.

From Jquery Docs of Hover()

The .hover() method, when passed a single function, will execute that handler for both mouseenter and mouseleave events.

So, your one function executing for both the events . You need to remove it after hover done

$("#selectorId").hover(
function() {
 //Do something to element here
},
 function() {
 //make your element normal here
 }
);

Or classic style.

 $("#cotsImg")
 .mouseover(function() { 
 $(this).attr("src", "/images/golfimagebw.png");
 })
 .mouseout(function() {
 $(this).attr("src", "/images/golfimage.png");
 });
Sergio
28.9k11 gold badges90 silver badges132 bronze badges
answered Aug 27, 2013 at 20:49
Sign up to request clarification or add additional context in comments.

Comments

0
<script type='text/javascript'>
$(document).ready(function(){
 $("#cotsImg").hover(function(){
 $('#golfImg').attr("src","/images/golfimagebw.png"); 
 $('#golfImg').attr("src","/images/golfimage.png");
 $('#golfImg2').attr("src","/images/golfimage2bw.png"); 
 $('#golfImg2').attr("src","/images/golfimage2.png");
});
});
</script>

just like that add what you want :)

answered Aug 27, 2013 at 20:43

1 Comment

You might want to put the second and last selector - attribute functions in the second hover callback. I think that's what he wants
0

If I understand your question, then you don't need more functions, just execute those lines of code excluding the functions.

answered Aug 27, 2013 at 20:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.