1

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);
        text_value = $(textbox).val();

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

What is happening is the text is appending when the enter/return key is hit (keyCode 13), but it is also moving the text a line down, as the enter/return key is supposed to.

This is occurring even though I set the value of the textbox to "".

TaylorMac
  • 8,882
  • 21
  • 76
  • 104

5 Answers5

4

How about event.preventDefault()

eugeneK
  • 10,750
  • 19
  • 66
  • 101
2

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

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
2

try this one event.stopImmediatePropagation()

$('#text_comment').live('keypress',function (e) {       
          if(e.keyCode == 13) {   
               e.stopImmediatePropagation()
       ///rest of your code
  }
});
Vivek
  • 10,978
  • 14
  • 48
  • 66
2

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("");
        }
        return false;
    }                                               
});

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 ?

Dhruva Sagar
  • 7,089
  • 1
  • 25
  • 34
1

Answer here http://jsfiddle.net/Z9KMb/

Carls Jr.
  • 3,088
  • 7
  • 37
  • 57
  • @TaylorMac- i think `ugeneK` has given the same answer,40 mins before, then for what you are waiting? working example :D – Vivek Jun 15 '11 at 09:50
  • lol, I think I know why, might be because he wanted to see the actual implementation before he believes on it. To see is to believe as how they say it. I would give +1 for eugeneK also for everyone who answered right for a credit. It doesn't matter who answered first, what matters most is we tried to help the best we can. :) – Carls Jr. Jun 15 '11 at 10:54
  • And +1 for TaylorMac for causing this trouble hahaha – Carls Jr. Jun 15 '11 at 10:55
  • @Ian Jasper Bardoquillo -+1 for your explanantion, why TaylorMac did this.. he he he – Vivek Jun 15 '11 at 10:58
  • @Vivek: hahaha this is what makes stackoverflow so exciting :) we learn a lot of things – Carls Jr. Jun 15 '11 at 11:07
  • @Ian Jasper Bardoquillo, it matters to me because i sell StackoverFlow points on ebay. – eugeneK Jun 15 '11 at 11:28
  • @eugeneK :) I'll take your word for it – Carls Jr. Jun 16 '11 at 01:37
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – ProgramFOX Mar 05 '14 at 15:59