2

I would like to bind a function to the mouseout event of my <canvas> element, and bind the same function to the blur and contextmenu events of my body. How would I go about binding this function to those elements at once, when there are different elements which need the same function bound to different events of each?

Thanks.

pimvdb
  • 151,816
  • 78
  • 307
  • 352

1 Answers1

6

You can't. Define your function beforehand:

function eventHandler(event) {}

and assign it separately:

$('canvas').mouseout(eventHandler);
$('body').bind('blur contextmenu', eventHandler);

With .bind you can at least bind one event handler to multiple events.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Yes I discovered that already, but using the 3 events to the 2 elements will e.g. also bind the function to the body's onmouseout event etc. But this will do, thanks. – pimvdb Jan 26 '11 at 14:49
  • @pimvdb: There is no other way. If you select both, `canvas` and body, then how can jQuery know which event to bind which element? It is just not possible. – Felix Kling Jan 26 '11 at 14:52
  • 1
    No, of course, so I asked for a possible way, but I understand there just isn't any. I'll just bind the function separately. – pimvdb Jan 26 '11 at 14:53