Link to home
Get AccessLog in
Avatar of elepil

asked on

JavaScript/jQuery: How to get the x-y coordinates of an element

I need to be able to center a dynamically-generated HTML element within a container that has both vertical and horizontal scrollbars. In this pursuit, I need to be able to get the container and target element's width and height (which I know how to do), and the target element's x-y coordinates. But when I try to access the target element's 'left' and 'top' properties, I get 'auto':

<!DOCTYPE html>
<script src="//"></script>
    $(function() {
        var cell = $("#cell");
        console.log(cell.css("left")); // returns 'auto'
        console.log(cell.css("top")); // returns 'auto'

        <tr><td><div id="cell">A Cell</div></td></tr>

Open in new window

Can anyone please show me how to get the 'left/top' or x/y coordinates of an HTML element? Thanks.
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of elepil


Thank you, Tom!
You're welcome. Thanks for the points.