15

I have div element with left and top defined, without absolute position, and I want to read the left and top values using jQuery.

Using $("#MyId").css("left") gave the expected result in IE browser (IE8) but in Chrome it returned "auto" instead, although the values are explicitly written in the element style.

Here is the test case: http://jsfiddle.net/qCDkb/2/

Note the difference between IE and Chrome.

Also, this is working well in jQuery 1.4.2 and "failing" in jQuery 1.4.3 and above.

Any insights are welcome. :-)

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 3
    It stands to reason that returning `auto` is the correct behaviour, as `position: static` doesn't listen to the `left` attribute. – Pekka Nov 25 '10 at 14:35
  • As verified here, notice the position is now 150px to the left. http://jsfiddle.net/qCDkb/3/ Position: relative added to the css. – Gazler Nov 25 '10 at 14:37
  • @Pekka - I don't want the actual position, for this I can use .position().left - I'm after the value defined in the CSS for that element.. – Shadow The GPT Wizard Nov 25 '10 at 14:37
  • Seems like a misuse of css. You should use data() for this. http://api.jquery.com/jQuery.data/ – Gazler Nov 25 '10 at 14:38
  • @Gazler - I don't want to actually position the element 150 pixels to the left. I want those values there only as "flag" for using later. Probably the wrong approach.. :/ – Shadow The GPT Wizard Nov 25 '10 at 14:39
  • @Shadow in that case, you should be able to get that using `$("#MyId")[0].style.left` but `data()` might be an even better idea as @Gazler suggessts – Pekka Nov 25 '10 at 14:39
  • @Pekka good point, but ( still can't understand why jQuery is failing to read this value in Chrome and succeeds in IE. – Shadow The GPT Wizard Nov 25 '10 at 14:46
  • @Shadow yeah. I added what I know (or think) as an answer, maybe it helps – Pekka Nov 25 '10 at 14:51
  • Also you should check your css. If you gave the element "right" property chrome and IE does not allow you to change the "left" property with jquery. – Serdar Akkılıç Aug 24 '12 at 13:23
  • @Serdar no, you are not correct. See [this live example](http://jsfiddle.net/JsYJm/) to prove you wrong. – Shadow The GPT Wizard Aug 24 '12 at 20:22

5 Answers5

15

Try $("your selector").position().top;

nicael
  • 18,550
  • 13
  • 57
  • 90
Steven Benjamin
  • 199
  • 1
  • 4
  • 1
    You can see this yourself [in the fiddle](http://jsfiddle.net/qCDkb/237/). Not working, always returning 0 as it returns the actual position while I want to read the value I have defined inline for the element. – Shadow The GPT Wizard Dec 10 '12 at 07:42
  • 1
    Worked for me in Chrome and FF. – Flat Cat Jan 05 '13 at 04:23
  • Note the "rendering" sequence comes into play here as well http://stackoverflow.com/questions/1324568/is-document-ready-also-css-ready – samus Dec 03 '15 at 17:41
13

It is strange behavior for jQuery. But you can use native javascript methods to get css values:

$("#Panel1")[0].style.left

This expression will return corresponding css property.

vtambourine
  • 2,109
  • 3
  • 18
  • 27
10

As discussed in the comments, setting left to auto for a position: static sounds somehow right, seeing as left has no meaning in the context.

As to why Chrome and IE return different values: .css() provides a unified gateway to the browsers' computed style functions, but it doesn't unify the way the browsers actually compute the style. It's not uncommon for browsers to decide such edge cases differently.

As to why jQuery 1.4.2 and 1.4.3 do this differently, I do not know for sure, but there's this in 1.4.3's release notes:

Nearly the entire CSS module has been rewritten focusing entirely on extensibility. You can now write custom CSS plugins that extend the functionality provided by .css() and .animate().

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

I know this is an old post, but I ran into this same problem and thought I would suggest a couple of solutions. It seems that this problem is not specific to Chrome, as I was able to reproduce in Firefox as well.

I was able to solve this one of two ways. Either place you CSS styles in the same file as your HTML, instead of using a separate CSS file. OR, call the function inside of window.onload. Looks like the values are not available to the browser until everything has loaded, IF the styles are in an external style sheet.

Hope this is helpful.

g3logic
  • 11
  • 2
0

add

position: absolute;

or

position: relative;

to the element you use left on

montrealmike
  • 11,433
  • 10
  • 64
  • 86