pimvdb
pimvdb

Reputation: 154958

Bind function to multiple events of different elements at once

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.

Upvotes: 2

Views: 1611

Answers (1)

Felix Kling
Felix Kling

Reputation: 817128

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.

Upvotes: 6

Related Questions