<p>I used jQuery to set hover callbacks for elements on my page. I'm now writing a module which needs to temporarily set new hover behaviour for some elements. The new module has no access to the original code for the hover functions.</p>

<p>I want to store the old hover functions before I set new ones so I can restore them when finished with the temporary hover behaviour.</p>

<p>I think these can be stored using the <code>jQuery.data()</code> function:</p>

<pre><code>//save old hover behavior (somehow)

$('#foo').data('oldhoverin',???)

$('#foo').data('oldhoverout',???);

//set new hover behavior

$('#foo').hover(newhoverin,newhoverout);
</code></pre>

<p>Do stuff with new hover behaviour...</p>

<pre><code>//restore old hover behaviour
$('#foo').hover($('#foo').data('oldhoverin'),$('#foo').data('oldhoverout'));
</code></pre>

<p>But how do I get the currently registered hover functions from jQuery?</p>

<p>Shadow2531, I am trying to do this without modifying the code which originally registered the callbacks. Your suggestion would work fine otherwise. Thanks for the suggestion, and for helping clarify what I'm searching for. Maybe I have to go into the source of jquery and figure out how these callbacks are stored internally. Maybe I should change the question to "Is it possible to do this without modifying jquery?"</p>

[{'answer_id': 49042, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m not sure if this is what you mean, but you can bind custom events and then trigger them.</p>\n\n<p><a href="http://docs.jquery.com/Events/bind" rel="nofollow noreferrer">http://docs.jquery.com/Events/bind</a></p>\n\n<p>So add your hover event, script the functionality you need for that hover, then trigger your custom event.</p>\n'}, {'answer_id': 49126, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 1, 'selected': False, 'text': '<p>Not sure if this will work, but you can try this:</p>\n\n<pre><code>\n&lt;!DOCTYPE html&gt;\n&lt;html lang="en"&gt;\n    &lt;head&gt;\n        &lt;meta charset="utf-8"&gt;\n        &lt;title&gt;Jquery - Get, change and restore hover handlers&lt;/title&gt;\n        &lt;script src="jquery.js"&gt;&lt;/script&gt;\n        &lt;script&gt;\n            function setHover(obj, mouseenter, mouseleave) {\n                obj.data("_mouseenter", mouseenter);\n                obj.data("_mouseleave", mouseleave);\n                obj.hover(obj.data("_mouseenter"), obj.data("_mouseleave"));\n            }\n            function removeHover(obj) {\n                obj.unbind("mouseenter", obj.data("_mouseenter"));\n                obj.unbind("mouseleave", obj.data("_mouseleave"));\n                obj.data("_mouseenter", undefined);\n                obj.data("_mouseleave", undefined);\n            }\n            $(document).ready(function() {\n                var test = $("#test");\n                setHover(test, function(e) {\n                    alert("original " + e.type);\n                }, function(e) {\n                    alert("original " + e.type);\n                });\n                var saved_mouseenter = test.data("_mouseenter");\n                var saved_mouseleave = test.data("_mouseleave");\n                removeHover(test);\n                setHover(test, function() {\n                    alert("zip");\n                }, function() {\n                    alert(\'zam\');\n                });\n                removeHover(test);\n                setHover(test, saved_mouseenter, saved_mouseleave);\n            });\n        &lt;/script&gt;\n    &lt;/head&gt;\n    &lt;body&gt;\n        &lt;p&gt;&lt;a id="test" href=""&gt;test&lt;/a&gt;&lt;/p&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>If not, maybe it\'ll give you some ideas.</p>\n'}, {'answer_id': 49434, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 3, 'selected': True, 'text': '<p>Calling an event <code>bind</code> method (such as <code>hover</code>) does not delete old event handlers, only adds your new events, so your idea of \'restoring\' the old event functions wouldn\'t work, as it wouldn\'t delete your events.</p>\n\n<p>You can add your own events, and then remove them without affecting any other events then use Event namespacing: <a href="http://docs.jquery.com/Events_(Guide)#Namespacing_events" rel="nofollow noreferrer">http://docs.jquery.com/Events_(Guide)#Namespacing_events</a></p>\n'}, {'answer_id': 49700, 'author': 'Erlend Halvorsen', 'author_id': 1920, 'author_profile': 'https://Stackoverflow.com/users/1920', 'pm_score': 0, 'selected': False, 'text': "<p>Maybe it would be easier to just hide the old element and create a clone with your event handlers attached? Then just swap back in the old element when you're done..</p>\n"}]
['https://Stackoverflow.com/questions/48931', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5114/']