faqts : Computers : Programming : Languages : JavaScript : Links

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

166 of 194 people (86%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I dynamically disable/enable a link?
does not work for NN 6 , at least!!!

Mar 23rd, 2000 17:14
Martin Honnen, comma geneus,


You can disable a link by setting its 
  onclick
handler to
  function () { return false; }
To enable it you set the
  onclick
handler to
  null
(or better the old onclick handler it had).
The following provides some functions which take care of it (plus 
changing the cursor style for IE4+ and NN6) together with two example 
links:

<HTML>
<HEAD>
<SCRIPT>
function cancelLink () {
  return false;
}
function disableLink (link) {
  if (link.onclick)
    link.oldOnClick = link.onclick;
  link.onclick = cancelLink;
  if (link.style)
    link.style.cursor = 'default';
}
function enableLink (link) {
  link.onclick = link.oldOnClick ? link.oldOnClick : null;
  if (link.style)
    link.style.cursor = 
      document.all ? 'hand' : 'pointer';
}
function toggleLink (link) {
  if (link.disabled) 
    enableLink (link)
  else 
    disableLink (link);
  link.disabled = !link.disabled;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<INPUT TYPE="button" VALUE="enable/disable link 1"
       ONCLICK="toggleLink(document.links[0]);"
>
<INPUT TYPE="button" VALUE="enable/disable link 2"
       ONCLICK="toggleLink(document.links[1]);"
>
</FORM>
<A HREF="http://javascript.faqts.com">
JavaScript.FAQTs.com
</A>
<BR>
<A HREF="http://php.faqts.com">
php.FAQTs.com
</A>
</BODY>
</HTML>