Becky
Becky

Reputation: 5585

using hover() on dynamically created elements

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
});

Upvotes: 0

Views: 1788

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388326

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
});

Upvotes: 3

Related Questions