HTML:
<a href="#" rel="tooltip-1">Open Tooltip</a>
<div id="tooltip-1">Tooltip Content</div>
jQuery:
xOffset = $('#tooltip-1').height() + 10;
yOffset = -30 ;
$("a[rel=tooltip-1]").hover(function(e){
this.t = this.attr("href");
$("body").append("<p id='tooltip'>"+ this.t +"</p>");
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
},
function(){
this.t = "";
$("#tooltip").remove();
});
$("a[rel=tooltip-1]").mousemove(function(e){
$("#tooltip-1")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
Two Issues:
- Tooltip div (#tooltip-1) doesn’t hide on mouseout.
- Since the tooltip div will always have the same ID as REL, I’d like to modify the above jQuery so that it takes target DIV ID automatically.
Thanks in advance for your help.
thisis a DOM node, not a jQuery wrapper: there is noattr()method.(I also don’t see why you’re writing to an expando
tproperty on the node. You don’t seem to use it anywhere else.)Try not to make HTML strings from plain text. Any
&or<characters intand your HTML is messed up. (This is an easy way to let cross-site-scripting security holes in.) Usetext()to set plain text content in an element instead.(Although, I’m not sure why you’re writing the
#attribute value into the tooltip. Are you trying to read the HTML of the tooltip div and copy it into thep? Why not just use the tooltip div itself?)It doesn’t hide/show at all, your script isn’t touching it anywhere.
How about putting this information in the
hreffragment, rather than abusingrel? Then the link actually points to the place it’s going to pop up.eg. something like:
(Or just use one of the many already-existing jQuery tooltip plugins.)