Skip to content Skip to sidebar Skip to footer

Trying To Reenable Link After Preventdefault Has Been Used

I have a link with the text 'No New Notifications'. The following code is used to make the link not clickable: if ($.trim($('a#full_notifications_link').text()) == 'No New Notific

Solution 1:

Adding a new event handler does not remove the event handlers you already have, so the default action is still prevented.

Use on() and off() instead

if ($.trim($('a#full_notifications_link').text()) == "No New Notifications"){

    $('a#full_notifications_link').on(function(event){
        event.preventDefault();
    });
}

$(document).ajaxComplete(function(){
    if ($.trim($('a#full_notifications_link').text()) == "See All Notifications"){

        $('a#full_notifications_link').off('click');

    }
});

It does seem easier to just check the text inside the event handler

$('a#full_notifications_link').on('click', function(e) {
     if ( $.trim($(this).text())  == "No New Notifications"){
         e.preventDefault();
     }
});

Solution 2:

Could you just move your logic into the click handler?

$('a#full_notifications_link').click(function(evt){
    if($(this).text() == "No New Notifications") {
        evt.preventDefault();
    }
});

Post a Comment for "Trying To Reenable Link After Preventdefault Has Been Used"