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?

3
  • Why not just removing the link if you don't want clicking upon it? Commented Dec 29, 2013 at 7:30
  • I can't because that code is coming from somewhere else and I have no control over it. Commented Dec 29, 2013 at 7:52
  • Please read this:stackoverflow.com/questions/11114622/… Commented Dec 29, 2013 at 8:04

6 Answers 6

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", "#");
 });
Sign up to request clarification or add additional context in comments.

1 Comment

This is still clickable but maybe this is really what he asked
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;
});

8 Comments

I tried this but didn't work. May be because there are multiple tags with same ID?
IDs are required to be unique, you must not do that. Use classes to group related tags.
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.
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.
Actually I am using SharePoint 2013 which is sending this HTML.
|
0

This is very simple.Just use attr in Jquery

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

Jsfiddle Demo here

6 Comments

I tried this but didn't work. May be because there are multiple tags with same ID?
Can you please explain me. What do you exactly want? If you want to disable all links with different ids... that is also possible
Yes I want to disable all links having this ID. And there are multiple such IDs on that page.
Hi have done it. I have disabled all links by using same class.Have a look on demo. JSFiddle Demo
I don't have css class in my element.
|
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();
});

Comments

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;
}

Comments

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"); 
    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.