0

I want to use jQuery's hover() on dynamically created elements. I tried the below but no success. How can I do this? I can not use mouseenter and mouseleave because this page will be embedded inside an iframe.

$(document).on('hover', '.dynamic', function(){
    //do
});
0

1 Answer 1

3

The hover() is not an event, it is a utility method used to register both mouseenter and mouseleave event handlers.

The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element.

So you can use mouseenter and mouseleave event handlers for the dynamic elements

$(document).on('mouseenter', '.dynamic', function () {
    //do
}).on('mouseleave', '.dynamic', function () {
    //do
});

If you want to have a single handler for both then

$(document).on('mouseenter mouseleave', '.dynamic', function () {
    //do
});
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. But mouseenter and mouseleave will not work when the page is embedded in an iframe?
@Becky if you see the above quote, the hover method just registers both these handlers... so if hover works, the above code also should work fine
I've read that mouseenter and mouseleave will not work when the page is embedded in an iframe. thank you Arun. I'll give it a go. :)

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.