GlassHalfAwesome
GlassHalfAwesome

Reputation: 873

jQuery .on with multiple events, multiple handlers and applying to new elements

I have a list that has items dynamically removed/added.

$('#myul').on('mouseenter', 'li', function() {
    //do something
});

but I want multiple events, each handled differently

$('#myul').on({
    mouseenter: function() {
        //do something
    },
    mouseleave: function() {
        //do something else
    }
});

I can't target $('.myul li') because that won't affect new list items. I tried sticking 'li', before function() but jQuery didn't like that. I also tried .hover but it had the same problem (doesn't apply to new list items.)

Is there any way to do this without having a different function for each event+handler?

Upvotes: 0

Views: 82

Answers (1)

dave
dave

Reputation: 64657

You put it after the event handler:

$('#myul').on({
  mouseenter:function(){...},
  mouseleave:function(){...}
} ,'li');

Upvotes: 1

Related Questions