# Get absolute position of an HTML element with JavaScript

**URL:** <https://forum.kirupa.com/t/get-absolute-position-of-an-html-element-with-javascript/330461>\
**Category:** Uncategorized\
**Created:** [January 25, 2013, 7:47am UTC](https://forum.kirupa.com/t/get-absolute-position-of-an-html-element-with-javascript/330461 "2013-01-25T07:47:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![galloschramme](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@galloschramme](https://forum.kirupa.com/u/galloschramme)\
**Post date:** [January 25, 2013, 7:47am UTC](https://forum.kirupa.com/t/get-absolute-position-of-an-html-element-with-javascript/330461/1 "2013-01-25T07:47:09Z")

</div>

I used the code provided [here](http://www.kirupa.com/html5/get_element_position_using_javascript.htm) to scroll to the top of an html element after an onclick-event.

The code works correctly only the second time when the users clicks the same link. The first time the link is clicked, the page actually does scroll, but not far enough. Does anyone know how to solve this? Thanks!

The javascript code is provided as attachment. The function toggleGalleryBox() runs when the user clicks a ‘li’ element. The section-tag should appear on top of the viewport by using the function scrollTo(0, Yposition).

The code is used for my personal website (see my profile).
