Copy Onclick event of an element

javascript, jquery

Solution

yes, you can access handler functions. The way of doing it in jquery would be this:

$('#specialbutton').click(function(){ console.log("peaches");};

var handlerFunction = $('#specialbutton').data("events").click[0].handler;

$('#specialbutton').click(); // "peaches"
handlerFunction(); // "peaches"

you would then assign it to another element like this:

$('#otherelement').click(handlerFunction);
$('#otherelement').click(); // "peaches"

Problem

hi to all i have an interesting question is it possible to copy onclick event of an element like this ``` $('#ElementId').attr('OldOnClick',$('#ElementId').attr('OnClick')); ``` Please guide me if there is any way of doing this. i am trying to disable click event of all elements present on form at some point and on some other point i have to recover them so i am trying to save their `onclick` with other attribute name

Original source

Related problems