jQuery Style How to - Get element position, left and top








Question

We would like to know how to get element position, left and top.

Answer


<!--  w  w  w .j  a  v a2s .co  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("*", document.body).click(function (e) {
                  var position= $(this).position();
                  e.stopPropagation();
                  console.log(position.left);
                  console.log(position.top);
           });
        });
    </script>
  </head>
  <body>
       <b>Hello asdf asdf</b>
  </body>
</html>

The code above is rendered as follows: