189

I use jQuery, I need to make some anchor tags perform no action.

I usually write it like this:

<a href="#">link</a>

However this refers to the top of the page!

Bryant Makes Programs
  • 1,493
  • 2
  • 17
  • 39
ahmed
  • 14,316
  • 30
  • 94
  • 127

19 Answers19

307

There are a few less than perfect solutions:

1. Link to a fake anchor

<a href="#">

Problem: clicking the link jumps back to the top of the page

2. Using a tag other than 'a'

Use a span tag and use the jquery to handle the click

Problem: breaks keyboard navigation, have to manually change the hover cursor

3. Link to a javascript void function

<a href="javascript:void(0);">
<a href="javascript:;">

Problem: breaks when linking images in IE

Solution

Since these all have their problems, the solution I've settled on is to link to a fake anchor, and then return false from the onClick method:

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

Not the most concise of solutions, but it solves all the problems with the above methods.

interjay
  • 107,303
  • 21
  • 270
  • 254
zaius
  • 6,409
  • 4
  • 28
  • 50
117

If you don't want to have it point to anything, you probably shouldn't be using the <a> (anchor) tag.

If you want something to look like a link but not act like a link, it's best to use the appropriate element (such as <span>) and then style it using CSS:

<span class="fake-link" id="fake-link-1">Am I a link?</span>

.fake-link {
    color: blue;
    text-decoration: underline;
    cursor: pointer;
}

Also, given that you tagged this question "jQuery", I am assuming that you want to attach a click event hander. If so, just do the same thing as above and then use something like the following JavaScript:

$('#fake-link-1').click(function() {
    /* put your code here */
});
littleibex
  • 1,705
  • 2
  • 14
  • 35
Tyson
  • 6,214
  • 3
  • 32
  • 37
  • 40
    This has a problem: it doesn't allow tab-navigation. –  May 29 '09 at 07:26
  • 7
    Note that (as Iraimbilanja points out) the span approach here disables the possibility to invoke the function using the keyboard, which I would consider a usability problem. – Fredrik Mörk May 29 '09 at 07:38
  • 5
    Also an accessibility problem. You can resolve it by using a button element (generated from JavaScript so non-JS users don't get a broken control). – Quentin May 29 '09 at 08:13
  • If it's a button that doesn't have a plain HTML fallback (links to a page or an anchor), then making it tab-navigable doesn't really serve much purpose for screen readers (of course, I could be wrong because I'm not too up-to-date on screen readers' JS capabilities). – Tyson May 29 '09 at 08:33
  • 4
    Here's a quick fix for that: tabindex="0" on a non-link element will enable keyboard navigation in most modern browsers. – Mathias Bynens May 29 '09 at 09:58
  • @Tyson Screen readers read what is on the screen, if the browser supports JS, then so does the screen reader. Of course, the JS might display information in a way that doesn't draw the attention of the reader, or do something that depends on sight to use - but that is a different issue. Generating the button from JS avoids presenting a useless control to users without JS. – Quentin May 29 '09 at 10:00
  • 1
    Another 'problem' is that you don't the CSS :hover pseudo class won't work on elements that aren't links in IE6. But since you're adding these elements through JavaScript anyway (at least, you should), you could just as well write a script that adds a .hover class to the element when hovered. – Mathias Bynens May 29 '09 at 10:01
  • 1
    Yes, this person obviously has no idea what semantic markup is, or has ever worked with accessibility – Eric Rowell Jul 29 '13 at 19:24
  • This is bad because I need the anchor to look like a standard default link (which may be different on different browsers) – Sanjay Manohar Feb 03 '18 at 19:43
50

To make it do nothing at all, use this:

<a href="javascript:void(0)"> ... </a>
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
39

The correct way to handle this is to "break" the link with jQuery when you handle the link

HTML

<a href="#" id="theLink">My Link</a>

JS

$('#theLink').click(function(ev){
    // do whatever you want here

    ev.preventDefault();
    ev.stopPropagation();
});

Those final two calls stop the browser interpreting the click.

goto
  • 7,908
  • 10
  • 48
  • 58
Oli
  • 235,628
  • 64
  • 220
  • 299
  • 5
    While that does sound like it's "proper" because it stops event bubbling, "return false" is a lot less verbose and does the same thing. The only time you would want to do that is if you already have event handlers registered to clicks on links via jQuery. – aleemb May 29 '09 at 08:26
34

There are so many ways to do it like

Dont add and href attribute

<a name="here"> Test <a>

You can add onclick event instead of href like

<a name="here" onclick="YourFunction()"> Test <a>

Or you can add void function like this which would be the best way

<a href="javascript:void(0);"> 
<a href="javascript:;">
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
24

What do you mean by nothing?

<a href='about:blank'>blank page</a>

or

<a href='whatever' onclick='return false;'>won't navigate</a>
KristoferA
  • 12,287
  • 1
  • 40
  • 62
16

This answer should be updated to reflect new web standards (HTML5).

This:

<a tabindex="0">This represents a placeholder hyperlink</a>

... is valid HTML5. The tabindex attribute makes it keyboard focusable like normal hyperlinks. You might as well use the span element for this as mentioned previously, but I find using the a element more elegant.

See: https://w3c.github.io/html-reference/a.html
and: https://html.spec.whatwg.org/multipage/links.html#attr-hyperlink-href

aross
  • 3,325
  • 3
  • 34
  • 42
  • 1
    This is also valid HTML4, but it won't style the anchor as a link. – interjay Jan 10 '13 at 13:23
  • 1
    I didn't know it was valid html4, but the HTML5 spec explicitly mentions the use-case. And styles which don't target the href attribute will work just fine, just the default browser styles may be an issue. – aross Jan 27 '13 at 15:08
  • 1
    This also doesn't trigger `click` event when user focuses the element and presses enter (tested in Firefox 51). – torvin Oct 09 '16 at 09:37
  • @torvin, that's simple. Use the `keydown` event. – aross Oct 10 '16 at 07:56
  • snazzy quirk, but in window 10 it adds a bordered box around the element – 1-14x0r Sep 29 '17 at 06:05
  • @KaraRawson what do you mean "in window 10"? What browser and version? Link to a jsfiddle or similar reproduction case? As mentioned before, default browser styles *might* be an issue, but usually a reset CSS can help with that, or simply overriding the offending rule. – aross Sep 29 '17 at 08:21
  • this was in an electron 1.76 application using Chromium. The front end was using react and semantic-ui. You were right about the default style.. it can be overridden with `outline:0;`'; – 1-14x0r Oct 01 '17 at 05:08
15

I think you can try

<a href="JavaScript:void(0)">link</a>

The only catch I see over here is high level browser security may prompt on executing javascript.

Though this is one of the easier way than

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

this should be used sparingly

Read this article for some pointers https://web.archive.org/web/20090301044015/http://blog.reindel.com/2006/08/11/a-hrefjavascriptvoid0-avoid-the-void

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Rutesh Makhijani
  • 17,065
  • 2
  • 26
  • 22
13

Here are the three ways for <a> tag's href tag property refer to nothing:

<a href="JavaScript:void(0)"> link </a>

<a href="javascript:;">link</a >

<a href="#" onclick="return false;"> Link </a>
Milan Gajjar
  • 701
  • 2
  • 14
  • 24
  • 1
    This is just a duplicate of [the currently highest voted answer](https://stackoverflow.com/a/925252/1000608), but with less info – aross Sep 11 '17 at 12:43
5

The only thing that worked for me was a combination of the above:

First the li in the ul

<li><a onclick="LoadTab2_1()" href="JavaScript:void(0)">All Assigned</a></li>

Then in the LoadTab2_1 I manually switched the tab divs.

$("#tabs-2-1").hide();
    $("#tabs-2-2").show();

This is because the disconnection of the also disconnects the action in the tabs.

You also need to manually do the tab styling when the primary tab changes things.

$("#secTab1").addClass("ui-tabs-active").addClass("ui-state-active").addClass("ui-state-hover").addClass("ui-state-focus");
    $("#secTab1 a").css("color", "#ffffff");
Nandan Acharya
  • 900
  • 2
  • 12
  • 24
pat capozzi
  • 1,412
  • 1
  • 20
  • 16
5

Make sure all your links that you want to stop have href="#!" (or anything you want, really), and then use this:

jq('body').on('click.stop_link', 'a[href="#!"]',
function(event) {
     event.preventDefault();
});
bearfriend
  • 10,322
  • 3
  • 22
  • 28
5

You can have an HTML anchor (a tag) without an href attribute. Leave off the href attribute & it won't link to anything:

<a>link</a>
tee
  • 4,149
  • 1
  • 32
  • 44
  • Thank you! It perfectly worked for my case. Where I wanted to have Anchor tag to open in new tab when there is URL, but nothing if no URL. In case of # it was accepting a CLICK and moving it to top of the page. Having no href part as you suggested. It solved the issue! Formatting looks great due to a tag, click issue gone with no href. Thank you!!! – Muhammad Asadullah Aug 16 '16 at 15:12
  • 1
    If anyone else is thinking if it is HTML5 valid, then answer is YES :) http://stackoverflow.com/a/33046203/5323892 . Here is from example from standards page (see the 3rd li item) : – Muhammad Asadullah Aug 16 '16 at 15:19
  • It works but if you want the mouse pointer to change to finger, just add href="JavaScript:void(0)" as suggested on @Rutesh Makhijani 's answer. – Tarik Aug 24 '16 at 07:14
5

I know this is an old question, but I thought I'd add my two cents anyway:

It depends on what the link is going to do, but usually, I would be pointing the link at a url that could possibly be displaying/doing the same thing, for example, if you're making a little about box pop up:

<a id="about" href="/about">About</a>

Then with jQuery

$('#about').click(function(e) {
    $('#aboutbox').show();
    e.preventDefault();
});

This way, very old browsers (or browsers with JavaScript disabled) can still navigate to a separate about page, but more importantly, Google will also pick this up and crawl the contents of the about page.

Connell
  • 13,925
  • 11
  • 59
  • 92
2

You can do it by

<a style='cursor:pointer' >Link</a>
1
<a href="#" onclick="SomeFunction()"  class="SomeClass">sth.</a>

this was my anchor tag. so return false on onClick="" event is not usefull here. I just removed href="#" property and it worked for me just like below

<a onclick="SomeFunction()"  class="SomeClass">sth.</a>

and i needed to add this css.

.SomeClass
{
    cursor: pointer;
}
Burk
  • 2,969
  • 1
  • 23
  • 24
  • What do u mean with that. What is recommended and why? – Burk Jan 26 '16 at 17:04
  • Its good practice to keep javascript and html in separate files, here's a detailed answer: http://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice – Alexandru Severin Jan 26 '16 at 17:13
1

I encountered this issue on a WordPress site. The headers on dropdown menus always had the attribute href="" and the header plugin being used only allowed standard urls. The easiest solution there was just to run this code in the footer:

jQuery('[href=""]').click(function(e){
    e.preventDefault();
});

This will prevent blank anchors from doing anything.

Bryant Makes Programs
  • 1,493
  • 2
  • 17
  • 39
0

React no longer support using a function like this href="javascript:void(0)" in your anchor tag, but here is something that works pretty well.

<a href="#" onClick={() => null} >link</a>
w3bh4ck
  • 71
  • 1
  • 3
-1

I know this is tagged as a jQuery question, but you can answer this with AngularJS, also.

in your element, add the ng-click directive and use the $event variable which is the click event... prevent its default behavior:

<a href="#" ng-click="$event.preventDefault()">

You can even pass the $event variable into a function:

<a href="#" ng-click="doSomething($event)">

in that function, you do whatever you want with the click event.

Jeremy Jao
  • 371
  • 3
  • 8
-4

In HTML5 just remove the href attribute

<a>Your text</a>
Ahmed Radi
  • 677
  • 1
  • 5
  • 20