Reputation: 183
I'm using a jQuery accordion and would like to save the existing click event for each accordion item, remove all the events, and add them back in when needed. I'm familar with attaching/detaching handlers using .on and .off, but how do I go about preserving the default handler for each of the accordion items without overriding it?
Upvotes: 1
Views: 583
Reputation: 69905
jQuery stores events and their handlers in data('events
)` with each element. You can try something like this.
var oldHandlers = $('#datepicker').data('events');
$('elementSelector').unbind();//will unbind all the handlers
oldHandlers
will be an object in this format.
{
click: [ {
handler: function(){.. }
..
},
{
handler: function(){.. }
..
}
..
]
focus: [ {
handler: function(){.. }
..
},
{
handler: function(){.. }
..
}
..
]
}
If you want to get the click
handler from oldHandlers you can say.
if(oldHandlers && oldHandlers.click && oldHandlers.click.length > 0){
$('elementSelector').click(oldHandlers.click[0].handler);
}
Upvotes: 1
Reputation: 15338
$('#foo').bind('click', function() {
alert('The quick brown fox jumps over the lazy dog.');
});
// will remove click event
$('#foo').unbind('click');
Upvotes: 0