Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable a href link in JavaScript?

I have a tag <a href="#"> Previous </a> 1 2 3 4 <a href="#"> Next </a> and in some conditions I want this tag to be completely disabled.

Code from comments (this is how the link is generated)

if (n_index != n_pages)      a = a+'<li><a href="#" onclick="javascript:paginateAjaxPage('+(n_index+1) +','+stype+');">></a></li><li><a href="#" onclick="javascript:paginateAjaxPage('+n_pages+','+stype+');" >>></a></li>';  else      a = a+'<li><a style="cursor: pointer;" onclick="return false;">></a></li><li><a style="cursor: pointer;" onclick="return false" >>></a></li>';  a = a+'</ul></div>'; 
like image 856
Warrior Avatar asked Mar 21 '11 10:03

Warrior


People also ask

How do you make a href disabled?

It is still possible to disable a link by following 3 steps: remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.

How do I deactivate a link?

To remove a hyperlink but keep the text, right-click the hyperlink and click Remove Hyperlink. To remove the hyperlink completely, select it and then press Delete.

How do you disable a link in Java?

To disable links: $("td > a"). attr("disabled", "disabled"). on("click", function() { return false; });

How do I turn off links on my website?

Disable links. Hold Ctrl+Alt to disable the links temporarily. This extension is deprecated - Chrome has a built-in feature for this, just holding Alt while selecting prevents the link from being clicked.


2 Answers

Try this when you dont want user to redirect on click

<a href="javascript: void(0)">I am a useless link</a> 
like image 70
Pranay Rana Avatar answered Oct 18 '22 04:10

Pranay Rana


you can deactivate all links in a page with this style class:

a {     pointer-events:none; } 

now of course the trick is deactivate the links only when you need to, this is how to do it:

use an empty A class, like this:

a {} 

then when you want to deactivate the links, do this:

    GetStyleClass('a').pointerEvents = "none"      function GetStyleClass(className)     {        for (var i=0; i< document.styleSheets.length; i++) {           var styleSheet = document.styleSheets[i]            var rules = styleSheet.cssRules || styleSheet.rules            for (var j=0; j<rules.length; j++) {              var rule = rules[j]               if (rule.selectorText === className) {                 return(rule.style)              }           }        }         return 0     } 

NOTE: CSS rule names are transformed to lower case in some browsers, and this code is case sensitive, so better use lower case class names for this

to reactivate links:

GetStyleClass('a').pointerEvents = "" 

check this page http://caniuse.com/pointer-events for information about browser compatibility

i think this is the best way to do it, but sadly IE, like always, will not allow it :) i'm posting this anyway, because i think this contains information that can be useful, and because some projects use a know browser, like when you are using web views on mobile devices.

if you just want to deactivate ONE link (i only realize THAT was the question), i would use a function that manualy sets the url of the current page, or not, based on that condition. (like the solution you accepted)

this question was a LOT easier than i thought :)

like image 28
Pizzaiola Gorgonzola Avatar answered Oct 18 '22 05:10

Pizzaiola Gorgonzola