Skip to content Skip to sidebar Skip to footer

How To Disable Enter/return Key After A Function Is Executed Because Of It?

I have this function where #text_comment is the ID of a textarea: $('#text_comment').live('keypress',function (e) { if(e.keyCode == 13) { textbox = $(this); te

Solution 1:

Solution 2:

Try and stop your event propagation (See http://snipplr.com/view/19684/stop-event-propagations/) when entering the if(e.keyCode == 13) case.

Solution 3:

try this one event.stopImmediatePropagation()

$('#text_comment').live('keypress',function (e) {       
          if(e.keyCode == 13) {   
               e.stopImmediatePropagation()
       ///rest of your code
  }
});

Solution 4:

I've tested this out, this works. The enter does not create a new line.

$('#text_comment').live('keypress',function (e) {

    if(e.keyCode == 13) {
        textbox = $(this);
        text_value = $(textbox).val();

        if(text_value.length > 0) {
            $(this).prev().append('<div id="user_commenst">'+text_value+'</div>');
            $(textbox).val("");
        }
        returnfalse;
    }                                               
});

Although I am wondering, if you don't want to ever have a new line, why are you using a textarea, why not use a input type='text' instead ?

Solution 5:

Post a Comment for "How To Disable Enter/return Key After A Function Is Executed Because Of It?"