views:

259

answers:

5

Hi,

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.

+8  A: 

Return false from onclick event:

<a href="#" onclick="fun(); return false;"> SomeText </a>
Anatoliy
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
Try adding the attribute: `onmouseover="window.status=''"` to display 'nothing' (an empty string) as the statusbar message.
Daan
onmouseover="window.status = '';"
Anatoliy
+2  A: 
<a href="javascript:;" onclick="fun();">Some Text</a>
<a href="javascript:fun();">Some Text</a>
Ghommey
+2  A: 
<a href="/SomePageWeDontWantToVisit" onclick="fun(); return false;">...</a>
Björn
Be careful with this approach though: if the client has disabled javascript, the page you didn't want to visit gets visited anyway :)
Daan
Well, yeah. Just wanted to make it more clear. :)
Björn
A: 

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>

stian
A: 

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

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

css

.link{cursor: pointer;}

edezacas