Duplicate:
I’ve got a textarea and a submit button:
<textarea id="chat"></textarea> <input type="submit" value="Submit" onclick="send_msg();">
The value of the of the texarea is submitted when Enter is hit:
textarea.onkeyup = function(e) { alert(e.keyCode); e = e || window.event; if (e.keyCode === 13) { send_msg(); } return true; }
Problem is to make the textarea submit its value prior to letting the cursor to jump to the next line. How’s it possible to do that?
The trick is to stop the browser from executing the default behaviour on key down and key up. To do that, simply
return falsein your onkeypress/onkeyrelease handlers when you detect keycode 13.