0

i want to disable link when i clicked on a link,here is my code:

<a href="?cmd=7" style="color:#00F; margin-left:15px; text-decoration:underline">Past 7 Days</a>
<a href="?cmd=14" style="color:#00F; margin-left:15px; text-decoration:underline">Past 14 Days</a>
<a href="?cmd=30" style="color:#00F; margin-left:15px; text-decoration:underline">Past 30 Days</a>
<a href="?cmd=custom" style="color:#00F; margin-left:15px; text-decoration:underline">Set A Custom Date Range</a>

i want when i click past 7 days link this link is disabled or ther links enabled,as soon then if i clicked on past 14 days link,past 7 day link is enabled and past 14 days link is disabled.how i do this?

Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56

7 Answers7

3
$('a').on('click',function(){
   $('a').removeAttr('disabled');
   $(this).attr('disabled',true);
})

or

.disable
{
 pointer-events: none;
 cursor: default;
}

$('a').on('click',function(){
    $('a').removeClass('disable');
    $(this).addClass('disable');
})
Prashobh
  • 9,216
  • 15
  • 61
  • 91
1

CSS:

a:visited
{
 pointer-events: none;
 cursor: default;
}
Dieter B
  • 1,142
  • 11
  • 20
  • From [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/:visited): "The :visited CSS pseudo-class lets you select only links that have been visited." That's not what the OP wants. – Tobias Jan 30 '14 at 10:56
0

use this code to disable using jquery

$("a").on('click',function(){
$(this).attr('disabled',true);
})
Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30
0

Try this:

$('a').on("click", function (e) {
    e.preventDefault();
});
Linga
  • 10,379
  • 10
  • 52
  • 104
0

Since my reputation doesn't let me comment yet, I'll post is as an answer here. This is intended to improove Karthick Kumar Ganesh's answer.

Depending on what jq-version you are using, you should use .prop() instead of .attr() for setting attributes / propperties.

Also I would use a class on your links if you're not just addressing anchors or adding get parameters, that should be disabled so that not all links are disabled after clicking.

$("a.disabable").on('click',function(){
  $(this).prop('disabled',true);
})

Lastly it makes perfect sense to add a target="_blank" or something similar to your links, so that your link opens in a new tab. Otherwise it makes no sense to me to disable a link, after it has been clicked.

An example link would look something like this

<a href="wher/you/want/to/go" class="disabable" target="blank" style="[...]">Foo</a>
Anticom
  • 975
  • 13
  • 29
0

This is the only solution so far that works across page loads as the OP described (I assume clicks on the anchors change the document location and therefore reload the page).

HTML (added class attributes):

<a class="cmd-7" href="?cmd=7" style="color:#00F; margin-left:15px; text-decoration:underline">Past 7 Days</a>
<a class="cmd-14" href="?cmd=14" style="color:#00F; margin-left:15px; text-decoration:underline">Past 14 Days</a>
<a class="cmd-30" href="?cmd=30" style="color:#00F; margin-left:15px; text-decoration:underline">Past 30 Days</a>
<a class="cmd-custom" href="?cmd=custom" style="color:#00F; margin-left:15px; text-decoration:underline">Set A Custom Date Range</a>

JavaScript (using the code from this answer):

$(function() {
  // Get the cmd query parameter
  var cmd = getParameterByName('cmd');
  if(cmd) {
    // Disable the link
    $('a.cmd-' + cmd).click(function(event) {
      event.preventDefault();
    })
    // Add a class to allow styling
    .addClass('disabled');
  }
});
Community
  • 1
  • 1
Tobias
  • 7,723
  • 1
  • 27
  • 44
  • when i used your code it gives error ReferenceError: getParameterByName is not defined – Muhammad Arif Jan 30 '14 at 12:16
  • @Arif I wrote "using the code from [this answer](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144)". So you should include the function declared in the referenced answer. – Tobias Jan 30 '14 at 13:06
  • i used your function but link not disabled – Muhammad Arif Jan 30 '14 at 13:12
  • how i disable link when it is clicked – Muhammad Arif Jan 30 '14 at 13:38
  • @Arif `event.preventDefault();` should have disabled the link. What does `getParameterByName('cmd')` return? You could also use CSS to disable the link: `a.disabled { pointer-events: none; cursor: default; }` (based on [this answer](http://stackoverflow.com/a/4416239/2948765)). – Tobias Feb 11 '14 at 15:53
0
.disabled {
text-decoration:none;
color:black;
 }


<a class="links" href="##"> Link1 </a></br>
<a class="links" href="##"> Link2 </a></br>
<a class="links" href="##"> link3 </a></br>


$(function(){
   $(".links").click(function(){
    if($(this).hasClass("disabled")){
         return false;
    }
    else{
        $(".links").removeClass("disabled");
        $(this).addClass("disabled");
    }
});
})

I think It will work

Pramod
  • 141
  • 3