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).

3
  • Check out this answer: stackoverflow.com/a/2970987/845632 See if that helps. Commented Sep 11, 2014 at 22:41
  • It's almost always wrong to bind one event handler inside another event handler, because you create multiple event handlers. Commented Sep 11, 2014 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. Commented Sep 11, 2014 at 22:44

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.