.position()

In this chapter you will learn:

  1. Syntax and Description for .position()
  2. Get the top and left position

Syntax and Description

.position()

gets the current coordinates of the first element in the set of matched elements, relative to the offset parent. Its return value is an object containing the properties top and left.

.position() method retrieves the current position of an element relative to the offset parent. .offset() retrieves the current position relative to the document.

Get the top and left position

<html><!--   j a  va 2  s . c o  m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("*", document.body).click(function (e) {
                  var position= $(this).position();
                  e.stopPropagation();
                  alert(position.left);
                  alert(position.top);
           });
        });
    </script>
  </head>
  <body>
    <body>
       <b>Hello java2s.com java2s.co m</b>
    </body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. Syntax and Description for .prepend()
  2. How to add text node with prepend
  3. Insert to a span element
  4. Insert a selected element