I have written jQuery plugin for copying events. Though, it only works with dynamically added events, but not those added with "on" function.
I hope this will help someone.
Parameters:
eventType - "click", "mouseover" etc.
destination - either jQuery object, dom element or selector
clearCurrent - if true it will clear current handlers at destination - default false
(function($){
$.fn.copyEventTo = function(eventType, destination, clearCurrent) {
var events = [];
this.each(function(){
var allEvents = jQuery._data(this, "events");
if (typeof allEvents === "object") {
var thoseEvents = allEvents[eventType];
if (typeof thoseEvents === "object") {
for (var i = 0; i<thoseEvents.length; i++){
events.push(allEvents[eventType][i].handler);
}
}
}
});
if (typeof destination === "string") {
destination = $(destination);
} else if (typeof destination === "object") {
if (typeof destination.tagName === "string") {
destination = $(destination);
}
}
if (clearCurrent === true) destination.off(eventType);
destination.each(function(){
for(var i = 0; i<events.length; i++) {
destination.bind(eventType, events[i]);
}
});
return this;
}
})(jQuery);