Jamie Hunter Jamie Hunter - 1 year ago 90
jQuery Question

jquery function running without being called

My basic setup is this:

function overLink ( e ){
alert( e );
}
$(document).ready(function() {
$( "#id" ).mouseover( overLink( this ) );
});


However 'overLink' is being called on page load, not when '#id' is hovered. I read that this is because of () in my function declaration, but I can't remove this as I want to pass a selector to the function. Any ideas?

Thanks!

Answer Source

What you're doing, when you "pass" this to the function like you are doing is to actually call the function. Try this instead

$(document).ready(function() {
    $("#id").mouseover(function() {
      overLink(this);
    });
});