Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get div left and right value using jquery

Tags:

jquery

There is no left assigned for the div, I use $('#').css('left'); to get the left value, but it shows auto.

How do I get the left value of the div.

Thanks Jean

like image 310
X10nD Avatar asked Nov 19 '10 20:11

X10nD


People also ask

How to get left position of div in jQuery?

jQuery position() Method The position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

What is offset() in jQuery?

jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.

What is the difference between position and offset in jQuery?

Difference between offset() and position() Method:The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element. When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method.


2 Answers

The offset() method tells you the actual position of the element relative to the document. See the example on the page that I linked to to see how they got the left and top values.

That is:

  var left = $('Your_selector').offset().left;
like image 40
Vincent Ramdhanie Avatar answered Oct 14 '22 07:10

Vincent Ramdhanie


The .css() command returns what the CSS setting is, so if you have 'auto' set, that is what you will see.

You can retrieve the computed position relative to the parent with $('#').position().left;, 'top' also is available from position(), if you want the value relative to the document use $('#').offset().left.

like image 140
Orbling Avatar answered Oct 14 '22 07:10

Orbling