How to get the top position of an element?

Is it possible to get the top position of an element using javascript/jquery ?

The element is a table, if that matters.


Solution 1:

If you want the position relative to the document then:

$("#myTable").offset().top;

but often you will want the position relative to the closest positioned parent:

$("#myTable").position().top;

Solution 2:

$("#myTable").offset().top;

This will give you the computed offset (relative to document) of any object.

Solution 3:

var top = event.target.offsetTop + 'px';

Parent element top position like we are adding elemnt inside div

var rect = event.target.offsetParent;
         rect.offsetTop;