Wingblade
Wingblade

Reputation: 10093

Preventing enter from creating a whitespace in a textfield?

So I got a textfield I use for a small chat program, and I let the chat send whatever is in a textfield by capturing the keydown event and sending the content of the textfield whenever enter is pressed. Problem is: Enter for some reason places a whitespace at the place where the user was last writing, so if the user clicks somewhere into the middle of the text in the textfield and then presses enter the message gets sent with a whitespace in the middle of it, which is very annoying... Any solutions?

Upvotes: 0

Views: 167

Answers (1)

Anoop
Anoop

Reputation: 23208

Use of preventDefault on enter will not allow any space in text area. HTML:

​<form action='www.google.com'>
<textarea id =textareaId>

</textarea>
</form>​

JS:

var ta = document.getElementById("textareaId");
ta.onkeydown = function(e){

    if(e.keyCode == 13){
        e.preventDefault();
        document.forms[0].submit();
    }
}

Upvotes: 1

Related Questions