1

How to disable the hyperlink based on the condition

 var mydiv = document.getElementById("myDiv");      
 var aTag = document.createElement('a');       
 aTag.setAttribute('href',"yourlink.htm");        
 aTag.innerHTML = "link text";      
 mydiv.innerHTML="";      
 mydiv.innerHTML=aTag;  

say i need to disable my aTag here.

Based on logged on user i need to disable or enable..

2
  • 1
    Based on what condition? Commented Aug 10, 2012 at 11:39
  • From UX POV, users are hardly used to disabled links, so I'd recommend against it. If you need something disabled, it most likely should've been button. Commented Aug 10, 2012 at 11:43

2 Answers 2

2

This should work

 if(condition)
     disableLink();
else
   showLink();



function disableLink()
        {

        document.getElementById('Link1').disabled=true;
        document.getElementById('Link1').removeAttribute('href');    
        document.getElementById('Link1').style.textDecoration = 'none';
        document.getElementById('Link1').style.cursor = 'default';
        }

        function showLink()
        {
            document.getElementById('Link1').disabled=false;
        //assign href dynamically
        document.getElementById('Link1').href = "somepage.html";
        document.getElementById("Link1").style.textDecoration = "underline";
        document.getElementById("Link1").style.cursor = "hand";
        }
Sign up to request clarification or add additional context in comments.

2 Comments

If i am not wrong Link1 is the id of the hyperlink right? But in the sample code as shown above i need to disable the aTag link. I tried the code provided above, but it was not effecting. Please suggest.
Isn't "assign href dynamically" a bit vague? You will lose the target URL when you call removeAttribute, so how do you get it back in the call of showLink? How about storing it before wiping it? There may be a better attribute to use than src but it worked for me: document.getElementById('Link1').src = document.getElementById('Link1').href;
2

You can disable the link by setting the disabled attribute (not valid on anchor elements, though it works in some cases).

Test setup for disabled attribute on anchors: http://jsfiddle.net/TgjnM/

Preferably, you can remove the link altogether and replace it with the text it contains. To replace the link with plain text, you would set the innerHTML of mydiv to the text (thus removing the hyperlink).

If the link can be toggled on/off, consider a form element (not a hyperlink) as @Oleg V. Volkov suggested in the comments. If it is a one-time decision (i.e. it won't be happening multiple times per page), I would replace the link with text.

Based on logged on user i need to disable or enable..

In that case, I would render the page differently on the server, not in the web browser.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.