Categories
JavaScript Answers

How to prevent a click on a ‘#’ link from jumping to top of page with JavaScript?

Spread the love

To prevent a click on a ‘#’ link from jumping to top of page with JavaScript, we set the href attribute to some JavaScript code.

For instance, we write

<a href="javascript:void(0);">link</a>

to set the href attribute to javascript:void(0); to stop the link from jumping when we click it.

void(0) does nothing so nothing will happen when we click the link.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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