# Get and set scroll position of an element using JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 12, 2020, updated Sep 23, 2021. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javacript-dom-get-and-set-scroll-position-of-an-element

> Learn how to get or set scroll position of an element by using scrollTop and scrollLeft properties in JavaScript.

To get or set the scroll position of an HTML element, you can use the `scrollLeft` and `scrollTop` properties.

The `scrollLeft` property sets or returns the number of pixels an element's content is allowed to scroll horizontally.

Similarly, the `scrollTop` property sets or returns the number of pixels an element's content is scrolled vertically.

Both these properties are relative to the top left corner of the element, which is by default `(0, 0)`.

The following example shows how to get the scroll position of an HTML element with a class `.wrapper`:

```javascript
const div = document.querySelector('.wrapper');

const x = div.scrollLeft;
const y = div.scrollTop;
```

To reset the scroll position of the `.wrapper` element, you can use the following code:

```javascript
const div = document.querySelector('.wrapper');

div.scrollLeft = 150;
div.scrollTop = 150;
```
