1

Can I do this:

$('.box').delegate('.edit', 'click', function(edit_event){   

  ... 
  var input = $('input', this);

  input.focus().bind('blur keypress', function(event){   

    // here disable the first .edit event (don't allow click on that element)?

  });

});

the event would be enabled again if certain conditions are met inside the 2nd event (and when a AJAX call is complete)

thecodeparadox
  • 81,835
  • 21
  • 131
  • 160
Alex
  • 60,472
  • 154
  • 401
  • 592
  • possible duplicate of [Best way to remove an event handler in jQuery?](http://stackoverflow.com/questions/209029/best-way-to-remove-an-event-handler-in-jquery) – Veger Aug 23 '11 at 15:18

1 Answers1

2

Since you're using the delegate()[docs] method, which is selector based, you could just add a class to the current .edit that excludes it from the selector.

 // only invoke if it has "edit" class and not "disable" class
$('#box').delegate('.edit:not(.disable)', 'click', function (edit_event) {

      // add the class to this edit element to disable it
    var edit = $(this).addClass('disable');
    var input = $('input', this);

    input.focus().bind('blur keypress', function (event) {

        // here disable the first .edit event (don't allow click on that element)?

        // after some work, remove the class to re-enable the click
        edit.removeClass('disable');
    });

});

I used the not-selector[docs] so that the click event won't fire until the disable class is removed.

user113716
  • 299,514
  • 60
  • 431
  • 433
  • but won't that click handler is still be attached to .edit until i remove it from the html? – Alex Aug 23 '11 at 15:24
  • @Alex: No, when you use `.delegate()`, you're only actually adding a handler to `#box`. When a click event that occurs inside `#box` bubbles up to it, `.delegate()` will run your `'.edit:not(.disable)'` selector against the nested element that was actually clicked to see if it matches. So when you click an `class="edit"` element, it will fire. But if you click a `class="edit disable"` element, it won't. Give it a shot, and let me know if it worked. :) – user113716 Aug 23 '11 at 15:27
  • @Alex: You're welcome. Yes, I didn't look at that comment closely enough. You could certainly add it there as well. It just wouldn't take effect until the first blur or keypress event occurs (as I'm sure you noticed). – user113716 Aug 23 '11 at 15:44