This post will discuss how to enable or disable an anchor tag in JavaScript and jQuery.

1. Using JavaScript – preventDefault() function

In pure JavaScript, this is very simple. To prevent an anchor from visiting the specified href, you can call the Event interface’s preventDefault() method on the anchor’s click handle. The code would look like this:

JS


HTML



Edit in JSFiddle

 
You can also add some CSS to make the link appear disabled to the end-user.

JS


HTML



Edit in JSFiddle

2. Using jQuery – Toggle anchor tag

To implement a toggle-like functionality with a click of a button, you can do like following. Note, this uses jQuery.

JS


HTML



Edit in JSFiddle

3. Using jQuery + CSS

With CSS, you can have a .disabled class that blurs the anchor tag. Then you can implement the toggle-like feature with jQuery by applying or removing this class on the anchor tag. On the anchor’s click handle, simply return false if that class is applied to the anchor. The idea is demonstrated below:

JS


HTML



Edit in JSFiddle

That’s all about enabling or disabling an anchor tag in JavaScript and jQuery.