0
function textarea_replace(that){

    var charnum = $(that).attr('data-char'),
    op = $(that).attr('data-op'),

    target = $(that).find('h2'),
    textarea = $(target).next('textarea'),
    testo = $(target).text();

    $(target).next('textarea').val(testo).show().focus();
    $(target).css({ 'display': 'none' });

    $(textarea).on({
        blur: function(e){
            e.stopPropagation();

            testo_fin = $(this).val()
            if (testo_fin.length > charnum) {

                var text_cut = testo_fin.substr( 0, charnum )
                $(this).prev().css({ 'display': 'block' }).text(text_cut)

            } else {
                $(this).prev().css({ 'display': 'block' }).text(testo_fin)
            }

            $(this).hide();
            $(textarea).off('blur');

            send_textarea(op, testo_fin, url_global);
        }
    });
}

I call it whith a event handler

$('.edit_box').on({
   click: function(e){ 
      $('.edit_box').off('click');
      that = $(this)
      textarea_replace(that);       
   }    
});

i don't understand how can i temporary stop and re active the click event, because if i click on the textarea it calls another event and send the text 2 (or more times).

Barmar
  • 741,623
  • 53
  • 500
  • 612
matt_matt
  • 27
  • 6
  • Check out this answer: http://stackoverflow.com/a/2970987/845632 See if that helps. – imtheman Sep 11 '14 at 22:41
  • It's almost always wrong to bind one event handler inside another event handler, because you create multiple event handlers. – Barmar Sep 11 '14 at 22:43
  • Bind all your handlers at top level. If you want to temporarily disable them, set a variable that they check before doing anything. – Barmar Sep 11 '14 at 22:44

1 Answers1

1

I'm not sure I really undestand what is supposed to happen here, but you can always use the .stop() method of jquery to clear the animation queue (or use clearQueue() if you added custom stuff)

$(".edit_box").click(function(){
    doSomething($(this));
}

function doSomething(what){
    what.stop() //Clears the queue, avoiding weird behaviour
    //Eventually change some properties, restore your textarea, etc.
    blur(what)
}

function blur(what){
    //Your stufff here
}
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164