2

For dynamically created content, I am using the 'on' method to handle events. That works fine with click but not with hover.

Here is the code:

$(document).on('click', 'li', function(){
    alert('a');
});

Works but

$(document).on('hover', 'li', function(){
    alert('a');
});

does not.

Is the code wrong? Or the problem arises cause of something else on my setup.

2
  • 1
    Hover is no real js event, its a jquery function. Thats why its not working. Commented Dec 20, 2016 at 6:41
  • 1
    delgates don't support hover event use mouse enter, mouse leave event . $(document).on(mouseenter: function(){}, mouseleave: function{}, 'li'); Commented Dec 20, 2016 at 6:44

2 Answers 2

4

"hover" pseudo-event

As of 1.9, the event name string "hover" is no longer supported as a synonym for "mouseenter mouseleave"

You need to use mouseenter and mouseleave event

$(document).on('mouseenter', 'li', function(){
    alert('a');
});
$(document).on('mouseleave', 'li', function(){
    alert('a');
});

You can also use alternate syntax

$(document).on({
    mouseenter: function () {           
    },
    mouseleave: function () {           
    }}, "li"); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. didn't know that hover can not be used.
2

Do not confuse the "hover" pseudo-event-name with the .hover() method

You should use mouseenter

$(document).on('mouseenter', 'li', function(){
    alert('a');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<ul>
  <li>sdada</li>
  </ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.