example fiddle
JavaScript
$('#btn').on('mouseover', function() {
$('#tt').show();
}).on('mouseleave', function() {
$('#tt').hide();
}).on('click', function() {
$(this).remove();
});
HTML
<button id="btn">press me</button>
<div id="tt">tooltip</div>
Basically, when you remove an element while your cursor is still over it, the mouseleave
event is never triggered. I guess this makes sense because if the element is gone, the event/binding is gone too.
But how do we work around it?
Sure, I could put $('#tt').hide();
in the click
event too, but I was hoping for a more generic solution. My real-life example is a bit more complex and I don't always know when the element is going to be removed.
There's no ondestroy
event or anything I can hook into, that will fire just before it's removed, is there?