I have some divs that are created with data from a json string. Most of them are images. Over those dynamic loaded images and want to show a div when mouse over it and hide when you mouse out. Therefore I used the live function, found it here on the forum.
The mouse over function works but it won’t mouse out. So when I hover one image the div shows up but when I mouse out the div is still visible. Any suggestions on this?
My code
<script type="text/javascript">
$('.product').live("hover", function() {
$(this).find('.product').stop(false,true); // get the image where hovering
$(this).find('div.caption').stop(false,true).fadeIn(100); // and fade in
},
function() {
$(this).find('.product').stop(false,true); // on mouse leave hide it
$(this).find('div.caption').stop(false,true).fadeOut(100); // fade out
});
</script>
UPDATED ANSWER
Thanks to the help below I found the solution.
$(".productsGrid .product").live("mouseenter", function() {$(this).find('.product').stop(false,true);
$(this).find('div.caption').stop(false,true).fadeIn(100);})
$(".productsGrid .product").live("mouseleave", function() { $(this).find('.product').stop(false,true);
$(this).find('div.caption').stop(false,true).fadeOut(100);});
The issue is that
liveonly takes one function parameter, not 2 in the way that thehoverfunction does. Usinghoverwithlivebasically just binds the one function to bothmouseenterandmouseleave.You can see an explanation and solution here.
However, unless you are using a version of jquery prior to 1.7, you should not be using
liveas it is deprecated. You should instead useon.Your code would look something like this:
Where
STATIC ANCESTORis replaced with an ancestor element of your.productelements which is not dynamically loaded. If neededdocumentcan be used but should only be used as a last resort.