128

I want a link that does nothing. I don't want this:

<a href="#">

because then the URL becomes something.com/whatever/#.

The only reason I want a link is so the user can see that they can click on the text. JavaScript is being used to perform some action so I don't need the link to go anywhere but I need it to look like a link!

I could use some data attribute and tell me CSS to make elements look like links if they have this attribute but it seems a bit overkill.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
ale
  • 11,636
  • 27
  • 92
  • 149

13 Answers13

148

The following will prevent your href from being ran

<a href="#" onclick="return false;">

If you are using jQuery, event.preventDefault() can be used

Curtis
  • 101,612
  • 66
  • 270
  • 352
128

Try this:

<a href="javascript:void(0);">link</a>
Weston Ganger
  • 6,324
  • 4
  • 41
  • 39
alexdets
  • 1,473
  • 1
  • 8
  • 3
  • 6
    +1 but quick question: is there a difference between `javascript:void` and `javascript:void(0)`? – Adam Rackis Nov 24 '11 at 17:21
  • 4
    If you will try use javascript:void(0) without param you will have SyntaxError and all your scripts will be failed. – alexdets Nov 25 '11 at 01:55
  • 3
    @alexdets `javascript:void()` will be SyntaxError, but `javascript:void` just returns undefined - same as `void(0)`. Adam, as far as I can tell, `void` and `void(0)` are functionally equivalent for this use. --- edit: although i see other comments that hint it might reload the page.. – andytuba Feb 27 '13 at 21:39
  • 3
    Does it matter how you capitalize javaScript/javascript/JavaScript/Javascript ? – Timothy Apr 22 '16 at 05:19
  • 2
    @TechyTimo No the capitalization does not matter, in-fact it should not be capitalized. I have submitted an edit. – Weston Ganger Jul 11 '17 at 17:31
  • 4
    This yields warnings, if your application uses [CSP Directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) that block inline script execution. Additionally, it's better to just write `javascript:;`, which has the same effect as your solution, but requires less characters to type. – Pawel Dobierski Sep 18 '19 at 22:13
57

In HTML5, this is very simple. Just omit the href attribute.

<a>Do Nothing</a>

From MDN on the a tag href attribute:

href

This was the single required attribute for anchors defining a hypertext source link, but is no longer required in HTML5.


What about the hand cursor on hover?

The default styles for a browser may not change the cursor to a pointer, for a tags with no href. You can universally change this with the following CSS.

a {
    cursor: pointer;
}
<a>Do Nothing</a>

However it's probably better to be more-selective about it, and apply it to only the elements you intend to add event handlers to.


What about making it tab-focusable?

Just add tabindex="0" to the element.

<a tabindex="0">Do Nothing</a>


Does it makes sense to use an a tag without a link?

Usually no, it's probably better to use a button element instead, and style it with CSS. But whatever you use, avoid using an arbitrary element like div when possible, as this is not semantic at all.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • 1
    With bootstrap 4, links that don't have an href don't get styled as links. – user3413723 Jul 17 '19 at 16:30
  • If you need to retain formatting due to some css definitions you can't fix or don't want to mess with, the href attribute maybe needed. The void(0) solution is better in that case. – Steve Horvath Jun 21 '21 at 01:15
  • Something really random to note: if you're worried about WCAG compliance, this approach won't satisfy their requirements – Dortimer Aug 19 '22 at 17:59
13

Proper:

<a href="#;">Link</a>
Michael Fever
  • 845
  • 2
  • 8
  • 26
  • 3
    Yes, '#' is defined as the top of the page, but any fragment string that doesn't match the id or name of an element in the document will do. https://html.spec.whatwg.org/multipage/browsing-the-web.html#find-a-potential-indicated-element – Chris Kerlin May 26 '22 at 22:56
  • 2
    This is the only answer that works when you want to specify a no-action link in markdown. For example, when you have a project on GitHub and you want to use GitHub badges in your README.md, as I am doing here: https://github.com/mikenakis/Bathyscaphe, and you want some of the badges to not link to anything when clicked. All other solutions will either take you to the top of the page, or take you to another page that shows just the badge. This is the only solution that really does nothing. – Mike Nakis May 29 '22 at 11:42
13

Don't make it a link (although it is prefered to do it) and style it with CSS so that it looks like a link:

p.not-a-link { text-decoration: underline; cursor: pointer } 

Or even better just make it a link and let the javascript function which is used e.preventDefault() to prevent the link.

Also add the link to the href so that users without JS enabled will still be able to use it (as a fallback).

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
11

<a href="javascript:;">Link text</a> - that's what I usually use

  • 6
    Won't this reload the page with some browsers? – Curtis Nov 24 '11 at 17:12
  • @Curt: You;re right, mis-read the question. You've got a comment upvote out of it :-) –  Nov 24 '11 at 17:15
  • ... and I've got 2 downvotes, including one after editing it! Harsh! –  Nov 24 '11 at 17:17
  • Remove the first code, then I'll be allowed to remove my down vote :) It wont let me remove the downvote, its "locked" – Curtis Nov 24 '11 at 17:23
  • Ta. My rep's up and down like Tower Bridge at the moment! –  Nov 24 '11 at 17:26
  • 1
    Which browsers does this affect, is it still an issue anymore? This would be preferred over `void(0)`, because it's surfaced in the status bar. I say that because I've users flag it as a bug/error (which it obviously isn't), so I think having it t is confusing to them. – patricknelson May 03 '18 at 02:08
4

We can achieve that using javascript void which normally involves evaluation of an expression and returning undefined, which includes adding javascript:void(0); on the href.

The void operator is usually used merely to obtain an undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

a {
  text-decoration: initial;
}
<a href="javascript:void(0);"> This link actually does nothing when clicked</a>
Krafty Coder
  • 91
  • 1
  • 5
2

one way which no one has mentioned is to point the href to an empty local file location like so

<a href='\\'>my dead link</a>

why? If you use a framework such as react or angular, the compiler will spit out some warnings which can make your log or console dirty. This technique will also prevent robots or spiders from incorrectly linking things.

1-14x0r
  • 1,697
  • 1
  • 16
  • 19
2

@Curt's answer will work, but you can use a cursor style in css to make it look like a link without the bother of generated a bogus link. Use hand or pointer depending on browser conformance.

Cross browser conformant pointer css (from cursor style guide):

element {
    cursor: pointer;
    cursor: hand;
}
amelvin
  • 8,919
  • 4
  • 38
  • 59
  • @PeeHaa Just use pointer only if you *don't* want it to work in IE5.5 and earlier! – amelvin Nov 24 '11 at 17:20
  • 3
    That's what I said! Please don't mention IE5.5 :P On a more serious note IE5.5 share is `0.17%`. If people still are on that version they don't deserve to browse the web – PeeHaa Nov 24 '11 at 17:23
-1

just remove the href attribute. it's not necessary.

<a> a link </a>
Amir Keramat
  • 384
  • 4
  • 10
-1

What if you use only css?

pointer-events: none; 

span, a {
    color: black;
    cursor: default;
    pointer-events: none;    
    text-decoration: none;
}
<span>Normal text --> <a href="https://google.com">Link to google click me</a> <-- another text</span>
mandel99
  • 106
  • 1
  • 10
-2

Text goes here When clicked this link will do nothing except display a little javascript:void(0) in the corner

ME ME
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 31 '22 at 15:12
-3

DONT USE <a>... instead use <span class='style-like-link'> and then use the class to style it however you want.

Bryce
  • 988
  • 9
  • 16
  • There's nothing wrong with using an anchor tag without a link itself, not to mention that there are many cases where it would make a lot _more_ sense to do so, rather than replace it with a pseudo-anchor tag. Not to mention styling one element to look like another but be implemented differently is bound to cause maintanence issues in the future – Tim Dec 08 '19 at 23:59
  • Agreed. It entirely depends on the use-case and what you're trying to accomplish. I merely provided this as an answer to help others know that this is AN option. – Bryce Dec 10 '19 at 00:05
  • If that's the case, I would suggest editing it to use language that shows it's a possible alternative, rather than stating in caps not to use a standard anchor tag for this, when it's perfectly valid to do so – Tim Feb 05 '20 at 02:19