0

I have the following HTML.

<a id="SomeLink" title="My Title" href="http://stackoverflow.com">Click Here</a>

I want to disable clicking on this link. Like if there is a way in CSS or Jquery to remove "href" from this link so it is not clickable?

Frank Martin
  • 3,147
  • 16
  • 52
  • 73

6 Answers6

1

Can you try this, Removed link during on ready handler, you can also use the same while in another firing event handler as well.

 $(function(){
      $("#SomeLink").attr("href", "javascript:void(0);");
 });

OR

 $(function(){
      $("#SomeLink").attr("href", "#");
 });
Krish R
  • 22,583
  • 7
  • 50
  • 59
1

Bind a click handler that does nothing and disables the default action by returning false.

$('#SomeLink').click(function() {
    return false;
});

Maybe this will allow you to deal with elements with duplicate IDs:

$('a[id=SomeLink]').click(function() {
    return false;
});

When you use an ID selector, jQuery uses getElementById, which will only find the first element with the ID. Perhaps using the generic attribute selector will bypass that and use a loop that just matches on the ID attribute. If that doesn't work, you may have to write a filter:

$('a').filter(function() {
    return this.id == 'SomeLink';
}).click(function() {
    return false;
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I tried this but didn't work. May be because there are multiple tags with same ID? – Frank Martin Dec 29 '13 at 07:44
  • IDs are required to be unique, you must not do that. Use classes to group related tags. – Barmar Dec 29 '13 at 07:48
  • I can't control that because code is coming from somewhere else. Is there a way to parse all those IDs that are coming in that page through javascript? Then I can run a look and remove HREF from each of them. – Frank Martin Dec 29 '13 at 07:50
  • I've added some suggestions to my answer. But you should complain to whoever is sending you the code, because it's invalid and will not work with many normal scripts. – Barmar Dec 29 '13 at 07:54
  • Actually I am using SharePoint 2013 which is sending this HTML. – Frank Martin Dec 29 '13 at 08:52
  • I don't know anything about SharePoint, but I suspect the problem is that the template author did not ensure unique IDs. – Barmar Dec 29 '13 at 08:55
  • This is SharePoint's default template which is made by Microsoft. – Frank Martin Dec 29 '13 at 10:10
  • Maybe you should ask about it on sharepoint.stackexchange.com. While Microsoft's flounting of standards is legendary, it seems unlikely they would intentionally do something like this. – Barmar Dec 29 '13 at 10:11
0

This is very simple.Just use attr in Jquery

function disbleLink(){
 $("#SomeLink").attr('href','');
 }

Jsfiddle Demo here

Abhinav015
  • 44
  • 7
0

Links cannot be disabled. Use a button instead.

<button disabled>click me</button>

If you want to prevent the default behavior when clicking a link, you can use e.preventDefault()

$("a").on('click', function(e){
   e.preventDefault();
});
chovy
  • 72,281
  • 52
  • 227
  • 295
0

You can use the following code in your JS.

$('#link').click(function(e){
e.preventDefault();
});

And this code in your CSS.

#link{
text-decoration:none;
color: #ccc;
}
Anil Kumar
  • 459
  • 6
  • 16
0

I found an answer that I like much better on this site.

Looks like this:

$(document).ready(function(){
    $("a#SomeLink").click(function () { 
        $(this).fadeTo("fast", .5).removeAttr("href"); 
    });
});

Enabling would involve setting the href attribute

$(document).ready(function(){
    $("a#SomeLink").click(function () { 
        $(this).fadeIn("fast").attr("href", "http://whatever.com/wherever.html"); 
    });

});

Shrey Gupta
  • 5,509
  • 8
  • 45
  • 71
shiny
  • 124
  • 6