Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable anchor using JavaScript? [duplicate]

I have to disable anchor link depending on a condition if some data comes to that field it should work as a hyperlink and if that data does not come then the link should not be there? Any ideas on this are welcome.

like image 219
krishna Avatar asked Apr 05 '11 13:04

krishna


2 Answers

Case 1:

To disable:

document.getElementById(id).style.pointerEvents="none";
document.getElementById(id).style.cursor="default";

To enable:

document.getElementById(id).style.pointerEvents="auto";
document.getElementById(id).style.cursor="pointer";

Case 2:

If you want the link to be gone (not just disable it):

document.getElementById(id).style.display="none";

to get it back:

document.getElementById(id).style.display="block"; //change block to what you want.

Case 3:

If you want to hide it preserving the space for it:

document.getElementById(id).style.visibility="hidden";

To get it back:

document.getElementById(id).style.visibility="visible";
like image 132
Jahid Avatar answered Oct 25 '22 19:10

Jahid


I couldn't make sense of your question body, so I'll answer your question's title...

How to disable anchor using javascript ?

JavaScript

if (condition) {
    document.getElementsByTagName('a')[0].removeAttribute('href');
}

jQuery

...because everyone uses it, right?

if (condition) {
    $('a').first().removeAttr('href');
}
like image 20
alex Avatar answered Oct 25 '22 19:10

alex