3

I have a left tree. Its structure is like this


    MainCategory
      Category1
        Subcategory1
        Subcategory2
      Category2
        Subcategory3
        Subcategory4
        Subcategory5
      Category3
      .
      .. etc like that      

If user click any of the MainCategory/Category/Subcategory I need to disable/prevent repeated/multiple click on same link until the result come. How can I do that using jquery?

DEVOPS
  • 18,190
  • 34
  • 95
  • 118
  • Can you include the jQuery you use to bind / attach to the click event ? – Manse Nov 10 '11 at 13:06
  • Been answered before -> http://stackoverflow.com/questions/1263042/how-to-temporarily-disable-a-click-handler-in-jquery – Manse Nov 10 '11 at 13:08

2 Answers2

2

if you use jQuery 1.7 you can use the off() and on() functionality

example:

var foo = function () {
    // code to handle some kind of event
};

// ... now foo will be called when paragraphs are clicked ...
$("body").on("click", "p", foo);


// ... foo will no longer be called.
$("body").off("click", "p", foo); 

if you use an older version you could unbind() and bind() the event

example:

var foo = function () {
    // code to handle some kind of event
};

// ... now foo will be called when paragraphs are clicked ...
$("body p").bind("click", foo);

// ... foo will no longer be called.
$("body p").unbind("click");
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
1

May be just use a class to check if its already clicked.
If clicked, do not execute, and remove the class on success and failure.

Prototype -

$('.selector').click(function(){
    if(!$(this).hasClass('someclass')){
        $(this).addClass('someclass');
        $.ajax({
          url: "test.html",
          success: function(){
            $(this).removeClass("someclass");
          },
          error: function(){
            $(this).removeClass("someclass");
          }
        });
    }
})
Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • this doesn't work: http://jsfiddle.net/manuel/XcYqJ/ event is already binded and doesn't check if the class is present or not – Manuel van Rijn Nov 10 '11 at 13:17
  • The check for the class is within the click event. So it does not prevent the click. but simply does not execute what is within the click. Please check the code. – Jayendra Nov 10 '11 at 13:19
  • sorry! my bad. however isn't beter to use the live event for this? then you don't have to check it by you're self if the class is set. once again sorry for not reading your code well enough :) – Manuel van Rijn Nov 10 '11 at 13:21
  • np :) ... Live would be if the elements are added dynamically. binding/unbinding or on/off as you suggested would be a good option. – Jayendra Nov 10 '11 at 13:25