2

Am trying to disable a hyperlink present inside a div tag using Jquery, i used the below codes, which are not helpful.

JQuery - 1.7v

html Code -

<div id="content">TESTING PURPOSE <(a)> href="/test/">Click Here <(a)> End </div>

jQuery - JS

$('#content').find("*").prop("disabled", true);

$('#content').prop("disabled", true);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
user2104391
  • 413
  • 4
  • 9
  • 18

5 Answers5

7

You can do this :

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

Returning false from the click event handler prevents the default behavior (following the link).

If you may have more than one link but you want to disable only this one, you may be more specific in your selector :

$('#content a[href="/test/"]').click(function(){ return false });
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    I liked this approach, less code that do more and not hurts the legibility the better (IMHO) – Roger Barreto Aug 24 '13 at 19:12
  • @RobSchmuecker You might need to read [the documentation](http://api.jquery.com/bind/) : *"Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object."* – Denys Séguret Aug 24 '13 at 19:47
2

Use preventDefault to disable the default behavior of a link.

Here is a little Code snippet:

$('#content a').click(function(e) {
  // stop/prevent default behavior
  e.preventDefault();

  // do other stuff...
  // e.g. alert('Link is deactivated');
});

Here is a little jsFiddle example

Difference between e.preventDefault and return false

Source: Stackoverflow - jquery link tag enable disable

Community
  • 1
  • 1
Michael J. Zoidl
  • 1,708
  • 16
  • 13
0

You can also do this, just give div name of that anchor tag with remove function.

$('.div_name a').remove();
Undo
  • 25,519
  • 37
  • 106
  • 129
0
$('#content a').click(function(e) {

  e.preventDefault();

});
Undo
  • 25,519
  • 37
  • 106
  • 129
OmShankar
  • 31
  • 2
0
$('.modal-body a').css({"pointer-events":"none"});
Undo
  • 25,519
  • 37
  • 106
  • 129
OmShankar
  • 31
  • 2