Prevent a link to reload page

It’s not the element that need a .preventDefault(), its the click event.

Try this:

$('nav.menu a').click(function (event) {
  event.preventDefault();
  // or use return false;
});

I don’t recommend to use the href as selector though, better to give it an id or name.

From MDN, about .preventDefault():
Cancels the event if it is cancelable, without stopping further propagation of the event.

You can read more here:


There is a CSS way, using pointer-events.

So by using in the CSS pointer-events: none; all click will be ignored. This is a “recent” alternative and suported in IE11+, Firefox 3.6+, Chrome 2.0+, Safari 4+.

Example

Leave a Comment