Posted By

chlab on 04/26/12


Tagged

event jquery copy EventHandler


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

BrockSamsom


Copy one element's eventhandlers to another


 / Published in: jQuery
 

URL: http://www.chlab.ch

Here's a snippet for copying (all) the eventhandlers of one element to another with jQuery and effectively cloning the behavior of one element.

Keep in mind that if it's possible, you should just register the same handlers you actually need, instead of just blindly copying all of them. But in some cases, e.g. when using a plugin that you do not wish to modify, this can come in handy.

  1. // iterate event types of original
  2. $.each($('#original').data('events'), function() {
  3. // iterate registered handler of original
  4. $.each(this, function() {
  5. $('#target').bind(this.type, this.handler);
  6. });
  7. });

Report this snippet  

You need to login to post a comment.