Javascript question: how do you disable a hyperlink?

edmicman

Golden Member
May 30, 2001
1,682
0
0
I'm using javascript and classic ASP; I want to programmatically disable a hyperlink on the page depending on what is in a textbox. I have it working where it sets the style property of the link to "disabled", graying the link out. This makes the link look disabled, but it still works. I changed the link to do a "void(0)" so it won't go anywhere, but isn't there a way I can actually *disable* the link altogether? Either changing the color of the link and making it so it doesn't do anything when you click, or make it not a hyperlink at all?

Or, can you change the onclick property to return false? It seems like I tried that and it didn't help, though. Any help would be awesome. Thanks!
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
I suppose you could use divs and make one that looked the way you wanted and wasn't a link. There's probably a better way, though.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
You can change the text in the area of the link:

<div id="linktext"><a href="http://linky">linky</a></div>


then when you want to change it:

document.getElementbyID("linktext").innerHTML = "just some text, no link"
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You can add an OnCLick to the link itself that calls a function that either returns true (if OK) or returns false and if you want shows an alert ("fill out the form first!")
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
One solution : Define a disabled hyperlink class and change the hyperlink style to that class.

For example:

 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: KB
You can change the text in the area of the link:

<div id="linktext"><a href="http://linky">linky</a></div>


then when you want to change it:

document.getElementbyID("linktext").innerHTML = "just some text, no link"

prbly the best way.
 

edmicman

Golden Member
May 30, 2001
1,682
0
0
Originally posted by: statik213
Originally posted by: KB
You can change the text in the area of the link:

<div id="linktext"><a href="http://linky">linky</a></div>


then when you want to change it:

document.getElementbyID("linktext").innerHTML = "just some text, no link"

prbly the best way.


Will that actually disable the link, or just change the text of the link? Hmmmm, I'll see if I can get that to work....

You could make it invisible.

This was my first solution - I had the link disappearing if it was invalid. Alas, the boss doesn't like things disappearing and reappearing on the page. Bah!
 

sunase

Senior member
Nov 28, 2002
551
0
0
>Or, can you change the onclick property to return false?

That seems to work fine for me. Example:
http://wai.dreamhost.com/test-disable_link.html

Is it just me, or is there no way to post HTML in a code section on this forum? Oo How tough is it to just convert the the less than and greater than to entities, geez.