I am writing a simple tooltip:
$(function() {
$('a').hover(function() {
var curLink = $(this);
var toolTipText = curLink.attr('title');
if (toolTipText)
{
var theOffset = curLink.offset();
$('body').prepend('<div id="toolTip">'+toolTipText+'</div>');
// how the heck is this working???
$('#toolTip').css({
'left' : theOffset.left+'px',
'top' : theOffset.top - 30+'px'
});
}
}, function() {
$('#toolTip').remove();
});
});
As you can see a div with the id of "toolTip" is dynamically added to the DOM when a user hovers over a link. That div isn't there initially when the DOM loads but is added later. So I assumed I had to use the live()
function and attach an event to it. But some how it works if I just treat it as a regular selector. Not that I'm complaining but how is this working?
You don't need live because that selector doesn't run before the element is in the DOM.
You would only need to use 'live()'
if your <a>
element wasn't in the DOM yet when you attach the events. IE. Any anchors added to your page after that code executes won't have tooltips.
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