I have a chat that uses textarea instead of text for obvious reasons. This is why each time members hit ENTER they get a new line instead of sending the message. I would like to change this, so every time they hit ENTER =the message to be submitted and then the cursor to return on textarea for their next message typing. I've tried different codes found on this site, most didnt work and those who seemed to do something were just refreshing the page and i got a blank page.
My code:
<form name="message" action="">
<textarea name="usermsg" autocomplete="off" type="text" id="usermsg" rows="4" cols="30" style="width: 450px; margin-left: 25px;">
</textarea>
<br/>
<p style="margin-left: 420px;"><input name="submitmsg" type="submit" id="submitmsg" value="Send" /></p>
</form>
I have created a jsfiddle with an example on how to do it with jQuery and the keypress
function and which
property: http://jsfiddle.net/GcdUE/
Not sure exactly what you are asking for more than this, so please specify your question further if possible.
$(function() {
$("#usermsg").keypress(function (e) {
if(e.which == 13) {
//submit form via ajax, this is not JS but server side scripting so not showing here
$("#chatbox").append($(this).val() + "<br/>");
$(this).val("");
e.preventDefault();
}
});
});