Yahreen Yahreen - 1 month ago 7
jQuery Question

Does jQuery offset(); work on td cells?

I'm trying to have a tool tip pop-up just slightly to the left of a couple of td cells in a table:

$('table.seafood td.prod4').hover(function() {
var offset = $(this).offset();
$("div.peekSeafood4").fadeIn(200);
$("div.peekSeafood4").css('left', offset.left + 'px');
}, function() {
$("div.peekSeafood4").fadeOut(200);
});


It's not working, throwing the tooltips far off to the side of the screen.

Does
offset();
not work with td cells/tables?

Answer

Yes, offset() works with table cells. This demo will show you it working at the most basic level.

I suspect the problem lies with $("div.peekSeafood4"), which will be positioned relative to its offsetParent element - you might need to make sure that the offsetParent for the matching element is the <body> element.

As @patrick_dw pointed out earlier, there's also the possibility that position() would give you the correct results, depending on where your tool tip pop-up is placed in the DOM.

Comments