1

Ok this might be condemned as a dumb question but I have been doing a lot of JQuery coding over the last few weeks and want to know if there is any real difference between using 'on' for just using the event it's self, e.g. click or submit.

So example, I have been do this most of the time,

 $('SECTOR-TAG-HER').on('click', function(event) {

      //DO STUFF HERE     

 }); 

But is it better to just do,

$('SECTOR-TAG-HER').click(function(event) {

    //DO STUFF HERE

});

I think it does not matter? But if one method is better than the other, I change to that method. Or does it not matter and is just based on the coder?

Thanks,

C0ol_Cod3r
  • 909
  • 2
  • 15
  • 35

2 Answers2

1

If you use the first approach, you can combine the

$('SECTOR-TAG-HER').on("event"

with a

$('SECTOR-TAG-HER').off("event"

so you get better control of the handlers.

opalenzuela
  • 3,139
  • 21
  • 41
1

.click() is just an alias to .on('click'). None is necessarily better. I usually go with .on('click') simply to keep it uniform to know that I'm declaring an event listener.

Almost forgot. With .on('click'), you can namespace your event like ".on('click.purpledino')". Then later you can unbind by just calling the namespace '.off(".purpledino")' if you have a set of bonded events with the same namespace

josephnvu
  • 1,210
  • 1
  • 9
  • 14