Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.delegate() single selector multiple events or is $.bind() a better solution?

So I am trying to find the answer to this, but there seems to be no definitive one I can find to help me make the right choice.

So What I am curious about is $.delegate() as of recently I am a fan of using it, seems to be a bit more stable in most of my apps now a days. So with that. I typically when using it will do something like

$('div').delegate('.someclass', 'click', function(e){ .... });

and I am good with that, until now. Where I have an element that I need to handle multiple events for. So I am wondering whats the best approach? do I do something to the extent of.

$('div').delegate('.someclass', 'click', function(e){ .... });
$('div').delegate('.someclass', 'mouseover', function(e){ .... });
$('div').delegate('.someclass', 'mouseout', function(e){ .... });

or do I go the $.bind() route? and do something like

$('.someclass').bind({
    click:function(){...},
    mouseout:function(){...},
    mouseover:function(){...}
});

does $.delegate() support something similar to $.bind()? It doesn't appear so reading through the API but ya never know (doesn't work for me). Or is there a function I simliar in nature to the combination of the two that I might not be aware of?

like image 381
chris Avatar asked Sep 14 '25 22:09

chris


2 Answers

.delegate and .bind have been superseeded by on.() as of jQuery ver 1.7

$('div').on({
    click:function(){...},
    mouseout:function(){...},
    mouseover:function(){...}
},'.someclass');

$('div').on( 'click mouseover mouseout','.someclass', function(e){ .... });

This approach can be used when you want to delegate the event..

like image 118
Sushanth -- Avatar answered Sep 16 '25 10:09

Sushanth --


Try this code. I have correct result view.

$('div').delegate('.someclass', 'click mouseover mouseout', function(e){ .... });
like image 30
evergreen Avatar answered Sep 16 '25 11:09

evergreen