3

Recently was working with image maps and trying to do something on hover of an image map. Naturally I tried .hover() first, but that didn't work and so when I tried .mouseover() that worked.

My question is, why does one work and not the other?

/*This function works*/
$(document).on('mouseover', '#some-map', function(){
    console.log('I am hovering with mouseover()');
}).on('mouseout', function(){
    console.log('I am no longer hovering with mouseover()');
});

/*This function does not work*/
$(document).on('hover', '#some-map', function(){
    console.log('This is from hover()');
}, function(){
    consoel.log('Out from hover()');
});

1 Answer 1

1

there ar no on('hover' ... method in jquery you can write it like this

$('#some-map').hover(function(){
   alert('This is from hover()');
}, function(){
   alert('Out from hover()');
});
2
  • Ahh, okay. That makes sense then. So how would I create a hover event for a dynamically created element?
    – Adjit
    Commented Nov 6, 2014 at 17:08
  • i always used to write like this: <button>Click</button> <script> $('button').on('click',function(){ $('body').append("<h1 id='some-map'>hello world</h1>"); $('#some-map').hover(function(){ alert('This is from hover()'); }, function(){ alert('Out from hover()'); }); })
    – kaxi1993
    Commented Nov 6, 2014 at 17:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.