How to disbale/enable some links using javascript dynamically?

C

cotton_gear

Hi,

Depending on the user input I need to disbale/enable some of the links
in my page. When disbled I need to display the links as normal text
with cursor changed to mouse pointer style and when enabled I need to
change it back to the normal link.
I tried using document.getElementsById('myLink').style but I am getting
'Object doesnt support this property...'
Can you suggest please ?

Thanks a ton.
CG
 
L

Lee

cotton_gear said:
Hi,

Depending on the user input I need to disbale/enable some of the links
in my page. When disbled I need to display the links as normal text
with cursor changed to mouse pointer style and when enabled I need to
change it back to the normal link.
I tried using document.getElementsById('myLink').style but I am getting
'Object doesnt support this property...'
Can you suggest please ?

<html>
<head>
<title>enable links</title>
<script type="text/javascript">
function returnTrue() { return true; }
function returnFalse() { return false }
function enableLink(linkid,enable) {
var link=document.getElementById(linkid);
if(enable) {
link.onclick=returnTrue;
link.style.textDecoration="underline";
link.style.cursor="pointer";
} else {
link.onclick=returnFalse;
link.style.textDecoration="none";
link.style.cursor="default";
}
}
</script>
</head>
<body>
<a id="alpha" href="http://www.google.com">google</a><br>
<a id="beta" href="http://www.wikipedia.com">wikipedia</a><br>
<a id="gamma" href="http://www.yahoo.com">yahoo</a><br>
<button onclick="enableLink('beta',false)">disable wiki</button><br>
<button onclick="enableLink('beta',true)">enable wiki</button><br>
</body>
</html>
 
R

RobG

cotton_gear said:
Hi,

Depending on the user input I need to disbale/enable some of the links
in my page. When disbled I need to display the links as normal text
with cursor changed to mouse pointer style and when enabled I need to
change it back to the normal link.
I tried using document.getElementsById('myLink').style but I am getting
-----------------------------------^

try:
document.getElementById(...


You only get one of 'em! You might also try using the links
collection rather than getElementById.

'Object doesnt support this property...'
Can you suggest please ?

One option is to have an A element and a span with plain text - hide
the A and show the span for disabled, swap for enabled.


Trivial example:

<a name="appleLink" href="http://www.apple.com">Apple</a><span
style="display: none;" id="appleText">Apple</span><br>

<input type="button" value="disable/enable link" onclick="
var aStyle = document.links.appleLink.style;
var sStyle = document.getElementById('appleText').style;
aStyle.display = ('none' == aStyle.display)? '' : 'none';
sStyle.display = ('none' == sStyle.display)? '' : 'none';
">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top