DOM replay event with jQuery

This is a purely theoretical question, so I am not looking for alternative solutions.

Is there a way to get the default handler to do something like this

var defaultHandler = $("#test").click; $('#test').unbind('click'); $('#test').bind('click', defaultHandler); 
+4
source share
1 answer

You can access the .data('events') object, which is used to store all the event handler information:

 $(document).ready(function() { var $test = $('#test'); $test.bind('click', function() { alert('default handler'); }); var storedClick = $test.data('events').click[0].handler; $test.unbind('click'); $('#restore').click(function() { $test.bind('click', storedClick); }); }); 

See this in action: http://www.jsfiddle.net/76GPF/

Remember that the events object is occupied by Arrays , so in the real world you have to store full information about the array. I just saved the very first handler in this example.

+6
source

Source: https://habr.com/ru/post/1336056/


All Articles