Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:36:57+00:00 2026-05-24T00:36:57+00:00

My question is related about disabling links/click events with jQuery, and it’s probably easier

  • 0

My question is related about disabling links/click events with jQuery, and it’s probably easier than it seems to me. I commented the 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 unbind all event using jquery

Result: It works, unbinding the click event from the anchors with delete-answer class, but:

1) It only deactivate the anchors with delete-answer class. I will prefer to disable all links while the servlet is doing it’s stuff.

2) I can’t (or I don’t know how to) re-enable the links.

Method 2: I tried the following method: How do I dynamically enable/disable links with jQuery?

Result: It works for normal anchors, but not for the anchors with class delete-answer.

Both seem incompatible, so I’d really appreciate some help.


Note: also tried to change the class doing this: $('.delete-answer').addClass('delete-disabled').removeClass('delete-answer');

It changes the class and leaves the anchors only with delete-disabled class, but when I click them again, they’re still deleting the message and I don’t know why :/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:36:57+00:00Added an answer on May 24, 2026 at 12:36 am

    Wrap all of that code in a function and use a flag.

    1. Add this at the top:

      (function() {
      
    2. Add this at the bottom:

      })();
      
    3. Just under the top line above, add:

      // Flag for whether "delete answer" is enabled
      var deleteAnswerEnabled = true;
      
    4. In your click handler, right at the top:

      if (!deleteAnswerEnabled) {
          return false;
      }
      
    5. Change your post to:

      // Disable deleting answers while we're doing it
      deleteAnswerEnabled = false;
      $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
           // Enable it again now we're done
          deleteAnswerEnabled = true;
      });
      

    Bringing that all together:

    // (1)
    (function() {
        // (3)
        // Flag for whether "delete answer" is enabled
        var deleteAnswerEnabled = true;
    
    
        $('.delete-answer').click(function(event) {
            event.preventDefault();
    
            // (4)
            // Don't do it if we're disabled
            if (!deleteAnswerEnabled) {
                return false;
            }
    
            // 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();
    
            // (5)
            // Disable deleting answers while we're doing it
            deleteAnswerEnabled = false;
            $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
                 // Enable it again now we're done
                deleteAnswerEnabled = true;
            });
    
            $('.output').empty();
            $('.output').append('Message deleted successfully.');
    
        });
    // (2)
    })();
    

    If you’re feeling sufficiently paranoid, you might use a counter rather than a boolean, but the concept is the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to my last one about jQuery 1.4. They supposedly fixed
Related question, about assignment-initialization-declaration. $ javac MatchTest.java MatchTest.java:7: ')' expected for((int i=-1 && String
This question is directly related to this SO question I posed about 15 minutes
My question is about restoring complex activity related data when coming back to the
This is related to this question I asked earlier about syntax highlighting user-defined blocks
Somewhat related to this question , but in the absence of any answer about
I was posting some comments in a related question about MVC caching and some
This is a question related to Objective-C memory management. On the About Memory Management
I can't find a related question to give me an idea about how to
Good evening fellow stackers, I have a mutex and threaded related question about //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.