title: Get the position of a fixed div with jquery
body: <p>I have a div with position "fixed" and I want to get the value of its position <strong>relative to the whole document</strong> while the user scrolls down the page.</p>
  
  <p>So, lets say I place the div on (x=0,y=0) and then the user scrolls down a bit and now, relative to the document, the div is on (X=0,y=300). I want to get that information, I want to know the exact position of that div in every moment, again, relative to the whole document, not to the window or the browser.</p>
  
  <p>I've tried many things but nothing seems to get what I'm trying to.</p>
  
  <p>One of them is this code, <strong>which does not work in the case of a fixed div</strong>:</p>
  
  <pre><code>var position = $("#fixed").offset(); /*it gets the position of the div
                                       "fixed" relative to the document*/
  $("#fixed").html(position.top);      /*it prints the obtained
                                       value on the div "fixed"*/
  </code></pre>
  
  <p><a href="http://jsfiddle.net/m4rk89/kVJ4x/" rel="noreferrer">Here you can find the running code</a>, and you can see that, when you scroll down, the value of the position of the div <strong>does not change</strong>.</p>
  
  <p>If I am not wrong, the code should print a new value on the div everytime it changes its vertical position relative to the document. But it turns out that it does not happen.</p>
  
  <hr>
  
  <h2><strong>SOLVED:</strong></h2>
  
  <p>The question was solved by <a href="https://stackoverflow.com/users/415057/codef0rmer">codef0rmer</a>. <strong>I was missing to track the scrolling</strong> to refresh the value of the position of the fixed div. I was an idiot. So <strong>the final code</strong> works fine the way he wrote it:</p>
  
  <pre><code>$(function () {
      var position = $("#fixed").offset();
      $("#fixed").html(position.top);
  
      $(window).scroll(function () {
         var position = $("#fixed").offset();
          $("#fixed").html(position.top);
      });
  })
  </code></pre>
  
  <p>And <a href="http://jsfiddle.net/kVJ4x/4/" rel="noreferrer">here you can see the running code.</a></p>
  
  <p>Thank you everyone and specially to codef0rmer.</p>