3

I want to create a hyperlink that does not link to any page. When clicked it executes a javascript function i have defined. So, i created a link as follows:

<a onclick="fun()"> SomeText </a>

But, the mouse pointer does not change to the hand symbol when we hover a mouse over the link.

So, i changed the link to

<a href="#" onclick="fun()"> SomeText </a>

So,now i get the hand symbol but now the location in the address bar changes to <url>/# whenever, i click on the link.

Is there a way to create a hyperlink not linking to any location, but the mouse pointer should change to the hand symbol on mouse hover over it ?

Thank You.

5 Answers 5

8

Return false from onclick event:

<a href="#" onclick="fun(); return false;"> SomeText </a>
3
  • Thanks for the reply, that worked ! :). Is there also a way to hide the link text that shows up in the status bar when the mouse is hovered over the link ?
    – Mike
    Oct 9, 2009 at 7:32
  • Try adding the attribute: onmouseover="window.status=''" to display 'nothing' (an empty string) as the statusbar message.
    – Daan
    Oct 9, 2009 at 7:35
  • onmouseover="window.status = '';"
    – Anatoliy
    Oct 9, 2009 at 7:35
2
<a href="javascript:;" onclick="fun();">Some Text</a>
<a href="javascript:fun();">Some Text</a>
2
<a href="/SomePageWeDontWantToVisit" onclick="fun(); return false;">...</a>
1
  • Be careful with this approach though: if the client has disabled javascript, the page you didn't want to visit gets visited anyway :)
    – Daan
    Oct 9, 2009 at 7:31
0

You can also add an event handler programatically, add something like this in your javascript onload section (e.g. window.onload()):

function clickHandler() {
  event.preventDefault();    
 // do some javascript stuff
}

document.getElementById("link").addEventListener("click", clickHandler, false);

Link in HTML page:

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

-1

It is more correct to create a span element, and modify css properties:

<span class="link" onclick="fun()"> SomeText </span>

css

.link{cursor: pointer;}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.