title: Jquery:how to get the location of each anchor link?
body: <p>How can i get location/position for each of these <code>&lt;a name="link1"&gt;&lt;/a&gt; , &lt;a name="link2"&gt;&lt;/a&gt;</code> etc etc</p>
  
  <p>for example when i click on <code>&lt;a href="#link1"&gt;link1&lt;/a&gt;</code></p>
  
  <p>i want to get the location/position on the page, ie the value of <code>&lt;a name="link1"&gt;&lt;/a&gt;</code>  and so forth.</p>
  
  <p>I have the following code , i want to get the position value*<em>(eg top =500px)</em>* of <code>&lt;a name="link1"&gt;&lt;/a&gt;</code> , <code>&lt;a name="link2"&gt;&lt;/a&gt;</code>etc  in the page and pass it on here</p>
  
  <pre><code>$('ul#mainNav li a').click(function (e) {
      e.preventDefault();
      if($(this).attr('href') !== '#'){
          var location = $($(this).attr('href')).offset().top - 150; /* For your header height, subtract 150 or whatever it ends up being */
          $('html, body').animate({scrollTop: location}, 600);
      } else {
          $('html, body').animate({scrollTop: 0}, 600);
      }
  });
  
  &lt;div id="wrapper"&gt;
  
      &lt;div id="branding"&gt;
      &lt;h1&gt;Branding&lt;/h1&gt;
      &lt;ul id="mainNav"&gt; 
      &lt;li class="first"&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt; 
      &lt;li&gt;&lt;a href="#link1"&gt;link1&lt;/a&gt;&lt;/li&gt; 
      &lt;li&gt;&lt;a href="#link2"&gt;link2&lt;/a&gt;&lt;/li&gt; 
      &lt;li&gt;&lt;a href="#link3"&gt;link3&lt;/a&gt;&lt;/li&gt; 
  
      &lt;/ul&gt; 
  
      &lt;/div&gt; 
  
      &lt;div id="content"&gt; 
  
  
  
          &lt;div id="form-b"&gt;
  
          &lt;form id="comments_form" action="#" method="post"&gt;
  
      &lt;fieldset&gt;
        &lt;legend&gt;Your Contact Details  Link1 &lt;a name="link1"&gt;&lt;/a&gt; &lt;/legend&gt;
  
        &lt;p&gt;
        &lt;label for="author"&gt;Name: &lt;span class="required"&gt;(Required)&lt;/span&gt;&lt;/label&gt;
        &lt;input name="author" id="author" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="email"&gt;Email Address: &lt;span class="feedback"&gt;Incorrect email address. Please try again.&lt;/span&gt;&lt;/label&gt;
        &lt;input name="email" id="email" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="url"&gt;Web Address:&lt;/label&gt;
        &lt;input name="url" id="url" type="text" /&gt;
        &lt;/p&gt;
      &lt;/fieldset&gt;
  
  
      &lt;fieldset&gt;
        &lt;legend&gt;Your Contact Details  Link 2&lt;a name="link2"&gt;&lt;/a&gt; &lt;/legend&gt;
        &lt;p&gt;
        &lt;label for="author"&gt;Name: &lt;span class="required"&gt;(Required)&lt;/span&gt;&lt;/label&gt;
        &lt;input name="author" id="author" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="email"&gt;Email Address: &lt;span class="feedback"&gt;Incorrect email address. Please try again.&lt;/span&gt;&lt;/label&gt;
        &lt;input name="email" id="email" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="url"&gt;Web Address:&lt;/label&gt;
        &lt;input name="url" id="url" type="text" /&gt;
        &lt;/p&gt;
      &lt;/fieldset&gt;
      &lt;fieldset&gt;
        &lt;legend&gt;Your Contact Details  Link 3&lt;a name="link3"&gt;&lt;/a&gt;&lt;/legend&gt;
        &lt;p&gt;
        &lt;label for="author"&gt;Name: &lt;span class="required"&gt;(Required)&lt;/span&gt;&lt;/label&gt;
        &lt;input name="author" id="author" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="email"&gt;Email Address: &lt;span class="feedback"&gt;Incorrect email address. Please try again.&lt;/span&gt;&lt;/label&gt;
        &lt;input name="email" id="email" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="url"&gt;Web Address:&lt;/label&gt;
        &lt;input name="url" id="url" type="text" /&gt;
        &lt;/p&gt;
      &lt;/fieldset&gt;
  
      &lt;fieldset&gt;
        &lt;legend&gt;Your Contact Details  &lt;/legend&gt;
        &lt;p&gt;
        &lt;label for="author"&gt;Name: &lt;span class="required"&gt;(Required)&lt;/span&gt;&lt;/label&gt;
        &lt;input name="author" id="author" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="email"&gt;Email Address: &lt;span class="feedback"&gt;Incorrect email address. Please try again.&lt;/span&gt;&lt;/label&gt;
        &lt;input name="email" id="email" type="text" /&gt;
        &lt;/p&gt;
  
        &lt;p&gt;
        &lt;label for="url"&gt;Web Address:&lt;/label&gt;
        &lt;input name="url" id="url" type="text" /&gt;
        &lt;/p&gt;
      &lt;/fieldset&gt;
  
      &lt;fieldset id="submit-b"&gt;
        &lt;legend&gt;  &lt;/legend&gt;
      &lt;div class="wrap"&gt;  
        &lt;p&gt;
      &lt;input id="submit" class="submit" name="submit" type="submit"/&gt;
      &lt;/p&gt;
      &lt;/div&gt;
      &lt;/fieldset&gt;
  
      &lt;/form&gt;
  
  
          &lt;/div&gt;
  
      &lt;/div&gt; 
  
  
  
      &lt;div id="footer"&gt;
      &lt;p&gt;Footer&lt;/p&gt;
      &lt;/div&gt;
  
  
      &lt;/div&gt;
  </code></pre>