4

I have one anchor tag. I want to disable it after once it clicked. I tried the following way.

  print "<script type='text/javascript'>parent.window.frames[2].document.getElementById('pagination').innerHTML+=\"<a href=# onclick='pageshow`($i);this.disabled=true'>$i</a>&nbsp;\"</script>";

Note: I don't want to disable the link. I want to disable the link operation. Like Google search page.

Eg: 1 2 3 4 5 Next

Here once I clicked the 1st page I can't click the same page again. Same thing I want.

Mike Lyons
  • 1,748
  • 2
  • 20
  • 33
user285146
  • 111
  • 1
  • 3
  • 7

5 Answers5

7

You can also disable the anchor tag by using the following way.

<a href='URL' style='text-decoration:none;cursor: auto;' onClick='return false'>LINK</a>
vivek
  • 141
  • 6
  • This is a slightly misleading answer, and doesn't answer the question I think most people coming here would be asking, which is how to disable the link once clicked, for example in a shopping cart checkout when you don't want a 'checkout' link to be clicked more than once. Your solution prevents the link from causing any navigation at all. See http://stackoverflow.com/questions/1164635/how-to-enable-or-disable-an-anchor-using-jquery – Neek Feb 11 '12 at 06:46
3

Having reread the question…

Google do not "Disable the link after it has been clicked".

They just don't link to the current page. Their markup is appalling so I won't copy it, a simplified example (for page 2) would be:

<ol>
  <li><a>1</a></li>
  <li>2</li>
  <li><a>3</a></li>
  <li><a>4</a></li>
</ol>  
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

The best answer I've seen to this question involves using jQuery.

How to enable or disable an anchor using jQuery?

here's the code incase that link breaks. This disables <a id="current"> by preventing e (the click event on the anchor)

$(document).ready(function() {
  $('a#current').click(function(e) {
    e.preventDefault();
  });
});
Community
  • 1
  • 1
Mike Lyons
  • 1,748
  • 2
  • 20
  • 33
  • In your case, I would make all the page number links disable conditionally, such as, when the page number is a#current or something. – Mike Lyons Apr 18 '12 at 23:42
1
<a href="…" onclick="this.removeAttribute('href');">
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I find this prevents even the first click from working, because by the time this function returns and the browser decides to act upon the click, the has no href. – Neek Feb 11 '12 at 06:47
0

Javascript isn't the right way to do this, but I'll provide an answer for the heck of it:

var links = document.getElementsByTagName('a'), link, children;
for(var i = 0; i < links.length; i++) {
    link = links[i];
    if(link.href == window.location.href) {
        children = link.childNodes;
        for(var j = 0; j < children.length; j++) {
            link.parentNode.insertBefore(children[j], link);
        }
        link.parentNode.removeChild(link);
    }
}

Edit: I don't know if "he-double-hockey-sticks" is "foul language" on SO but if it is, this will be deleted mysteriously some months from now so I've used "heck" instead.

eyelidlessness
  • 62,413
  • 11
  • 90
  • 94