Search code examples
jqueryposition

jQuery - getting position() of the li within ul


I'm trying to figure out how to get the left position of the li relative to its ul parent tag. Any idea?


Solution

  • You can make the ul tag the li's offset parent by giving it position: relative. This will allow you to call position on the li, which will tell you what you want to know.

    var pos = $("#target").position();
    $("#target").text("This li is positioned " + pos.top
                    + "px from the top and " + pos.left
                    + "px from the left edge of the ul");
    

    See it in action.