My question is about disabling click links / events using jQuery, and this is probably simpler than it seems to me. I commented on important code to make it easier.
I have the following code in a .js file:
$('.delete-answer').click(function(event) { event.preventDefault(); // some actions modifying the tags $('.output').closest('li').remove(); var idMsg = ...; var action = ...; var answers = ...; $(this).closest('li').children('p').remove(); $(this).closest('.tr').before('<tr><td><div class="output">Deleting message...</div></td></tr>'); $(this).closest('.tr').remove(); // While the servlet is deleting the message, I want to disable the links // but I can't, so my problem is just here // METHOD 1 //$('a').unbind('click'); // METHOD 2 //$('a').bind('click', function(e){ // e.preventDefault(); //}); $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { }); // METHOD 1 //$('a').bind('click'); // METHOD 2 //$('a').unbind('click'); $('.output').empty(); $('.output').append('Message deleted successfully.'); });
And in my HTML, I have some list items like these:
<li> <p class="text">Some text.</p> <table class="answer-details" style="width: 100%"> <tbody> <tr class="tr"> <td style="width: 50%;"> <div class="msg-modification" display="inline" align="right"> <a id="modify" class="delete-answer" href="#">Delete</a> </div> </td> </tr> </tbody> </table> </li>
As you can see, I tried two methods to disable the click event:
Method 1: I tried the following method: how to unleash all events using jquery
Result: it works by detaching the click event from the anchors with the delete-answer class, but:
1) It deactivates anchors only with the delete-answer class. I prefer to disable all links while the servlet is doing this.
2) I cannot (or do not know how) to re-enable links.
Method 2: I tried the following method: How to dynamically enable / disable links using jQuery?
Result: it works for regular anchors, but not for anchors with the delete-answer class.
Both seem incompatible, so I really appreciate some help.
Note. Also tried to change the class: $('.delete-answer').addClass('delete-disabled').removeClass('delete-answer');
It changes the class and leaves the bindings only with the delete-disabled class, but when I click on them again, they still delete the message, and I don't know why: /