Reputation: 5432
My mind is boggled. Can anyone tell me why this works with 'mouseenter' but not with 'click'?
//////THIS DOESNT WORK
$('#view_panel').on('click', 'img.main_image', function(){
$(this).parent().find('div.image_box').show();
});
$('#view_panel').on('click', 'img.main_image', function(){
$(this).parent().find('div.image_box').hide();
});
/////THIS WORKS
$('#view_panel').on('mouseenter', 'img.main_image', function(){
$(this).parent().find('div.image_box').show();
});
$('#view_panel').on('mouseleave', 'img.main_image', function(){
$(this).parent().find('div.image_box').hide();
});
Upvotes: 1
Views: 162
Reputation: 33870
It is not that it's not working, you just call both function when you click.
Try that instead (1 click function):
$('#view_panel').on('click', 'img.main_image', function(){
$(this).parent().find('div.image_box').toggle();
});
Upvotes: 1
Reputation: 8918
The second event being bound is firing immediately after the first one. So the div is being hidden as soon it is shown.
Try using toggle()
instead.
Upvotes: 3