2

I have to enable and disable the anchors based on index ,eg : when index is 3 or more &gt anchor should be disabled . I have written the following code snipet but the anchor is not getting disabled when index is 3 .

if(index == 3){
     $("#anchor2").click(function(e){
                e.preventDefault(); 
                return false;
        });
 }
Aditya
  • 401
  • 1
  • 7
  • 17

5 Answers5

2

You can just modify the scenario as:

    index=3;//change the value andcheck the result
    $('#anchor2').click(function (e) {
        if (index == 3) {
            e.preventDefault();
            return false;
        } else {
            alert('I am Enabled Now');
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id="anchor2" href="#">Click me!</a>
Tushar
  • 11,306
  • 1
  • 21
  • 41
1
$('#anchor2').click(function (e) {
    if (index >= 3) {
        e.preventDefault();
    }
    //if index<3 follow the link normally
});
Tom Sarduy
  • 16,482
  • 8
  • 64
  • 83
1

try this trick:

<a href="#" data-disabled >Click Here! disabled</a>

$('a[data-disabled]').click(function (e) {
    e.preventDefault();
    console.log('disabled');
});

just put a data-disabled attribute on your 3rd anchor tag.

jsfiddle Demo

Mohamad Shiralizadeh
  • 7,153
  • 5
  • 52
  • 78
1

You can do this css trick:

if(index == 3){
    $("#anchor2").css('pointer-events', 'none');
}
Nabscalyx
  • 21
  • 4
0

Try this:

<a href='javascript:void(0);'>some text</a>
Bugs
  • 4,356
  • 9
  • 30
  • 39
Shiv Kumar Sah
  • 1,014
  • 1
  • 13
  • 16