Is there a way to backup all of the events that are set on any given element?
Backstory: I've got some code that shows an absolutely positioned 'popup' of sorts when a user mouses over a certain icon (think on-hover help text). If the user mouses over the popup, it sticks around, but if they mouse off of the popup or the icon, the popup hides. This is all working as intended. However the popup also has some form elements inside of it (don't ask) and if the user starts to fill out the form, I'd like to remove the mouseleave event that hides the popup so that they don't accidentally mouse off and lose their data. Terrible UX aside, this is fairly easy to do, but the kicker is that I want to re-add that mouseleave functionality once the user submits the form or clicks the icon.
Pseudocode:
var event_backup;
var icon = $("img#icon");
var popup = $("div#popup");
icon.bind("mouseenter"):
popup.show();
icon.bind("mouseleave"):
popup.hide();
icon.bind("click"):
popup.hide();
popup.events = event_backup; //how do I do this?
popup.bind("mouseleave"):
popup.hide();
popup.bind(...):
//lots of bind events for popup that I want to keep
//I'd rather not duplicate code and manually re-add these later
popup.children("form input").bind("focus"):
event_backup = popup.events; //how do I do this?
popup.unbind();
popup.children("form input#done_button").bind("click"):
popup.events = event_backup; //how do I do this?
Hopefully that makes sense, and apologies if this is a dumb question... it's been a long day.
Might be better to just have a flag, and at the beginning of relevant functions perform a simple check if you can:
if (skipHover) return;
If in any case you want access to bound events:
popup.data('events').click
is an array of bound functions, and so too for other event names.
Instead of putting all your code inside of bind you could try separating it out into it's own function. That way it should be easier to bind and you can also reference it. for example:
var popupBind = function(el) {
//some code in here, however long you want
//we can reference the element triggering the event with 'el'
}
//now we can use this in our bind calls
popup.bind("click",
function() {
popupBind($(this));
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With