I have included the below code in the header of my page:
JavaScript
x
8
1
<script type="text/javascript">
2
3
jQuery('#start_price').mouseleave(function() {
4
alert('Hi!');
5
});
6
7
</script>
8
I have tested it by putting my mouse into the text field with this ID and removing it, clicking outside of it but nothing fires. I checked the error console and there is nothing, so it looks like it’s not even attempting to fire it.
What am I doing wrong?
Advertisement
Answer
Timing is everything when registering event handlers. You need to wait until document ready to do this:
JavaScript
1
7
1
jQuery(document).ready(function(){
2
jQuery('#start_price').mouseleave(function() {
3
alert('Hi!');
4
});
5
6
});
7
Check out the updated fiddle: