Categories: Programming & Coding

Simple Tip : Disable Scrolling To Top On Link Click

Whenever we have a hyperlink which does not take you to the different page and its value is “#” then we generally get into this problem that – on clicking on hyperlink, page url is appended with # and focus is moved to top of the page.

Javascript Solution: 

To avoid page focus moving to top and page getting scrolled to top, we can return 0 when a anchor or hyperlink is clicked on a webpage.

Javascript Avoid Scroll To Top On Link Click

So just change the value from “#” to “javascript:void(0)”

<a (0)”>Sample Hyperlink</a>

Now with above trick, click will happen and user will be at same place instead of scrolling to top.

You might be thinking whats the use of this ?

We can write onclick event and perform some operation or we can write javascript or jquery handler which will perform some operation.

But in any case our focus will be at the same position, instead of scrolling and getting lost.

JQuery Solution :

If you are using JQuery and want to achieve same functionality then in JQuery we can call “event.preventDefault()” method to block default behavior.

So whenever we write click event of any anchor tag / hyperlink then at the end of the function, if we write event.preventDefault() then it will do the work.

If you know any other solution then let me know, I would love to add it here.

ProWebGuru

Mostly I write about technology related stuff on https://www.prowebguru.com Recently I have started making videos also. Mostly blog posts & videos are related to technology, programming and learning some new tips and tricks related to windows, wordpress, google app script, technical, programming, javascript, jquery and other coding related stuff. Youtube channel - https://www.youtube.com/user/prowebguru

View Comments

  • Please don't link to javascript:void(0). It can break the page's navigation in strange ways, especially when you try to open the link in a new tab or new window. Using a real valid url and calling the preventDefault method on the event object is the best solution by far. If you are unable to do this for some reason, reconsider why you are using the element, perhaps a or or something else would be better for your scenario.

Recent Posts

GPT 5.4: Powerful AI Breakthrough – 7 Game-Changing Features, Launch Date & Full Guide

GPT 5.4: The Next-Generation AI Model Transforming Reasoning, Coding, and Productivity Artificial Intelligence is evolving…

1 day ago

GPT‑5.3 Instant Explained: Faster, Smarter & Fewer Refusals

GPT-5.3 Instant is OpenAI's latest ChatGPT model update, prioritizing smoother conversations, fewer refusals, and higher…

2 days ago

Free Perplexity Pro for Airtel Customers: Unlock Advanced AI Assistance at No Cost!

Are you tired of scrolling endlessly through Google results trying to find clear, trustworthy answers?…

4 months ago

Unlocking Collaborative Coding with GitHub Copilot Spaces

In recent years, the process of software development has rapidly evolved—developers demand smarter tools and…

4 months ago

ChatGPT Go Now Free in India: Opening Doors to Advanced AI Capabilities

India has emerged as one of the fastest-growing and most significant markets in the global…

4 months ago

AgentKit by OpenAI: Redefining the Future of AI Agent Development

On October 6, 2025, OpenAI unveiled AgentKit, a unified suite of tools designed to help developers and…

5 months ago

This website uses cookies.