Skip to content

Instantly share code, notes, and snippets.

@travismillerweb
Created March 30, 2014 16:31
Show Gist options
  • Save travismillerweb/9875420 to your computer and use it in GitHub Desktop.
Save travismillerweb/9875420 to your computer and use it in GitHub Desktop.
JS - Duplicate and Copy Element's Methods
/*
JS - Duplicate and Copy Element's Methods
Take a method associate with an existing HTML element and add it to another element.
Reference Link: https://forum.jquery.com/topic/how-do-i-copy-the-click-event-from-one-element-to-another.
Tip: Know how your mouseevents or other actions work, you may need to break down certain steps in order to get the functionality you require.
*/
$('.new-element').each(function(i, value){
$(this).on("mousedown", function(){
var myclick, clicks = $._data( $(".existing-element")[i], "events" ).click;
//clicks is an object of aribtrarily keyed functions
//assuming you want the first, do
$.each(clicks,function(i,f) {
myclick = f; //f is the function
return false; //break the loop
});
//bind myclick to another element;
$(this).mouseup(myclick);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment