Prevent Default Behaviour of Hashed Anchor Tag with jQuery

Way back in the day, I use to always set up my event triggering anchor tags a certain way to prevent their default behaviour of having to click-through to a URL or named anchor:

Trigger

Usually, a more simple approach was to use a simple hash:

Trigger

That’s the way I should’ve been doing it, but I really hated how it always made the browser window jump to the top.
Solving that wasn’t too complicated. It just took a bit of jQuery.


With that code in place, the browser no longer jumps to the top when using a hashed anchor tag.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie