views:

986

answers:

2

I've been given a table based layout to work on and I need to display some tool tips above some input boxes contained in the table.

I can't use any of the offset properties to do this since they all return 1 when the element is contained in a table. Does jQuery or some other library have a way of calculating this?

+3  A: 

What about the offset method, which returns the pixel offset of the element (relative to the document)?

var offset = $('input', myTableRow).offset();
// access offset.left and offset.top properties

You can use this value to position other elements at the same spot:

$(myTooltipElement).appendTo(document.body).css(offset)
Roatin Marth
@Roatin I have one question though, suppose that it's not just contained in one table...say its contained in about 5 or 6 layers of tables (thanks firebug!), then how do you calculate it?
leeand00
Oh wait I know...I just keep passing in elements right?
leeand00
Not sure I know what you mean. Getting to the right element boils down to using the right selector. The one in my answer will actually find all `input` elements inside that tr, **anywhere**, even nested inside a table inside another table. You need to change it to get the exact input(s) you need.
Roatin Marth
+1  A: 

Try this:

var offset = $(myobject).position().left;
$(mytooltip).css('left',offset + 'px');

Of course you'd have to have the tooltip's position set to absolute and top (or bottom) position defined.

fudgey
Why would you have to set `top` to zero for IE?
Crescent Fresh
Oops I messed up and I meant that it needs to be defined - I was thinking about something else IE does LOL.
fudgey