How do I make a link that goes nowhere?

HtmlHyperlink

Html Problem Overview


Is it possible to make a link that does nothing?

I want the item to be a link so I get the cursor to change on mouseover, but I don't want the link to actually go anywhere as it actually triggers the showing of a div with extra functionality on the same page.

Html Solutions


Solution 1 - Html

Will add to the browser history:

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

Won't add to the browser history (preferred method):

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

Solution 2 - Html

Instead, you could use a <span> and set the cursor style:

<span id="node42" class="node-link">My Text</span>

And specify the cursor in your stylesheet:

.node-link { cursor: pointer; }

This would also allow you to attach to the node for your actions later (show here using Prototype):

<script type="text/javascript">
    $('node42').observe('click', function(event) {
      alert('my click handler');
    });
</script>

Solution 3 - Html

In my opinion, the most complete hack free solution is:

<a href="" onclick="return false;">do absolutely nothing</a>

Solution 4 - Html

Just add the style cursor:pointer to the element:

<a id="clickme">Click Me</a>

CSS:

#clickme { cursor: pointer }

Or (heaven forbid) in-line:

<a style="cursor:pointer">Click Me</a>

Solution 5 - Html

Here are two classic JavaScript approaches that won't give you an error in your JavaScript console and/or don't change the URL nor save it to your browser history:

<a href="javascript:void(0);">Click on this JavaScript error-free link that won't change your URL nor save it into your browser history</a><hr />

Maybe a more readable approach, but unfortunately this one changes your URL

Solution 6 - Html

If you just want style, set the the CSS cursor property to pointer.

But it sounds like you want <a href="javascript:void(do_something())">.

Solution 7 - Html

I was looking for something simpler, and I think I found it. Do you want your link not to go anywhere? Omit the href.

<a class='irrelevant_for_this_example'>Some text</a>

href="#" will scroll to the top of the page.

href="javascript:;" seems dodgy.

A JavaScript solution seems unnecessary for something as simple.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkurView Question on Stackoverflow
Solution 1 - HtmlleepowersView Answer on Stackoverflow
Solution 2 - HtmljheddingsView Answer on Stackoverflow
Solution 3 - HtmlpstantonView Answer on Stackoverflow
Solution 4 - HtmlDoug NeinerView Answer on Stackoverflow
Solution 5 - HtmlMysticalView Answer on Stackoverflow
Solution 6 - HtmlRoger PateView Answer on Stackoverflow
Solution 7 - HtmlMiguel MaderoView Answer on Stackoverflow