2

I read somewhere that it is possible to do something like this:

$(document).on({click: function(){}})

Is this true or is there something similar to this that jQuery supports? I would like to pass an object with settings.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
skmasq
  • 4,470
  • 6
  • 42
  • 77

1 Answers1

5

Yes, it is possible. It is an easy way to bind more than one event at once, like this:

$(target).on({
    click: function() { /*... do something when user clicks ...*/ },
    mouseover: function() { /*... do something else ...*/ },
    keyup: function(){ /* ... */ }
    // and so on...
});

You can even do this to bind a function to multiple events:

$(target).on('click mouseover keyup', function() { 
    // something to handle all events... 
});

See .on() documentation on jQuery

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 1
    related [Passing multiple events to trigger same function jQuery](http://stackoverflow.com/questions/2534089/jquery-multiple-events-to-trigger-the-same-function) – Nipuna Aug 03 '14 at 02:16