YourChild
May 7th 2008, 5:17 pm
I have a textarea. the user types in there and when he's done, he presses enter on the keyboard.
a function in javascript is called, which receives the text in the textarea and does something with it.
when the user hits enter, the text in the textarea is cleared out and the textarea appears blank again.
the problem is..my cursor drops to the next line after the user hits enter.
I want the cursor to remain at the beginning on the first line of the textarea. How do I do this?
here is my html code for the textarea:
<textarea name="inputMessage" id="inputMessage" wrap="virtual" onkeydown="handleKeyIM(event, this.id)" rows="10" cols="50"></textarea><br/>
here is the javascript:
function handleKeyIM(e, elemID){
e = (!e) ? window.event : e;
code = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
if (e.type == "keydown") {
if(code == 13){
var elem = document.getElementById(elemID);
var text = elem.value;
//do something with text ...
elem.value = '';
}
}
}
a function in javascript is called, which receives the text in the textarea and does something with it.
when the user hits enter, the text in the textarea is cleared out and the textarea appears blank again.
the problem is..my cursor drops to the next line after the user hits enter.
I want the cursor to remain at the beginning on the first line of the textarea. How do I do this?
here is my html code for the textarea:
<textarea name="inputMessage" id="inputMessage" wrap="virtual" onkeydown="handleKeyIM(event, this.id)" rows="10" cols="50"></textarea><br/>
here is the javascript:
function handleKeyIM(e, elemID){
e = (!e) ? window.event : e;
code = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
if (e.type == "keydown") {
if(code == 13){
var elem = document.getElementById(elemID);
var text = elem.value;
//do something with text ...
elem.value = '';
}
}
}