How to scroll to an element using JavaScript?

Spread the love

Sometimes, we want to scroll to an element using JavaScript.

In this article, we’ll look at how to scroll to an element using JavaScript.

How to scroll to an element using JavaScript?

To scroll to an element using JavaScript, we use the scrollIntoView method.

For instance, we write

document.getElementById("divFirst").scrollIntoView();

to select the element to scroll to with getElementById.

And then we call scrollIntoView on it to scroll to the element.

Conclusion

To scroll to an element using JavaScript, we use the scrollIntoView method.

Leave a Reply

Your email address will not be published. Required fields are marked *