-1

I have a <textarea> that onkeypress ('Enter') sends a message in a live chat. The problem is that after pressing first time "Enter", the textarea field starts from the second input row.

How do I make the field reset or not taking "Enter" as a next row value?

Code:

<textarea disabled = "enabled"
onblur = "stopTyping();"
onfocus = "playTitleFlag=false; 
window.title='';"
onkeypress = "tryToSend(event);"
id = "chatmsg"
rows = "1"
cols = "1"
class = "chatmsg"></textarea>

And the onkeypress function:

function tryToSend(event) {
    var key = event.keyCode;

    if (key == "13") {
        sendMsg();
        return;
    }

    var msg = document.getElementById("chatmsg").value;

    if (trim(msg) != "") {
        typing();
    }
    else {
        stopTyping();
    }
}
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93

1 Answers1

2

To cancel the default behaviour you should use return false;

see What's the effect of adding 'return false' to a click event listener?


To reset a textarea simply set its value to "". document.getElementById(f).value = "";


P.S Note that event.keyCode return an integer

Community
  • 1
  • 1
TastySpaceApple
  • 3,115
  • 1
  • 18
  • 27