jQuery method of calculating the correct offsetLeft of an element in a table
-
06-07-2019 - |
Question
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?
Solution
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)
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow