...

View Full Version : set focus inside of textarea



chump2877
08-03-2006, 09:20 PM
I'm trying to figure out how to return focus to the very first line of the textarea...currently, when the "enter" key is pressed, the value of the textarea is reset to an empty string, but the focus goes to the second line of the textarea instead of the first....here is my code:


<HTML>
<HEAD>
<TITLE></TITLE>

<script type="text/javascript">
<!--

function submitenter(e)
{
if (e.shiftKey && e.keyCode == 13)
{
return true;
}
else if (e.keyCode == 13)
{
document.add_line.messagetext.value = "";
document.add_line.messagetext.focus();
return false;
}
else
{
return true;
}
}

-->
</script>

</HEAD>

<BODY>

<form name='add_line'>
<p>Type in a message:</p>
<textarea name='messagetext' onKeyPress='submitenter(event);'></textarea>
</form>

</BODY>
</HTML>

Thanks for any help.

tom123
08-03-2006, 10:48 PM
Maybe browser recogonises line as 13 (carriage return) and 10 (line feed)?
test 10 in your script.

just a wild guess!

chump2877
08-04-2006, 12:12 AM
Maybe browser recogonises line as 13 (carriage return) and 10 (line feed)?
test 10 in your script.

just a wild guess!

The problem isn;t the javascript keycode here (as far as i can tell)....substituting 13 with 10 only made the script less functional than it already is....

the problem has something to do with the "enter" key moving focus to the second line...but I reset the value of the textarea to an empty string (before i reset focus to the texarea), so that should eliminate any extra line feeds?

chump2877
08-05-2006, 02:20 AM
No one knows the answer to this question?? It seems like it should be a no-brainer for the right person....thanks.

_Aerospace_Eng_
08-05-2006, 03:42 AM
Its not that no one knows, its that people have lives and can't answer every single question. Questions slip away. You just need to learn to be patient or start doing some searching on your own.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script type="text/javascript">
<!--
function submitenter(e)
{
if (e.shiftKey && e.keyCode == 13)
{
return true;
}
else if (e.keyCode == 13)
{
var val = document.add_line.messagetext.value;
var re = new RegExp("\n","g");
var newVal = val.replace(re, "");
document.add_line.messagetext.value = newVal;
document.add_line.messagetext.value = "";
document.add_line.messagetext.focus();
return false;
}
else
{
return true;
}
}
-->
</script>
</head>

<body>
<form action="#" name="add_line" id="add_line">
<p>Type in a message:</p>
<textarea name="messagetext" onkeyup="submitenter(event);"></textarea>
</form>
</body>
</html>

chump2877
08-05-2006, 04:05 AM
Its not that no one knows, its that people have lives and can't answer every single question. Questions slip away. You just need to learn to be patient or start doing some searching on your own.


Lol...Did someone wake up on the wrong side of the bed this morning? I didn;t think I was being offensive or necessarily "impatient" in my previous post, and that was quite an abrasive response [to my post]....And you think I didn;t try to scrounge up the answer to this question myself? I assure you I did some significant searching....time to bump it down a notch, chief...*sigh*

That being said, you were kind enough to reply to this post, and I do appreciate that...I'll implement your suggestions and see if it works for me...thanks! :thumbsup:

chump2877
08-05-2006, 04:47 AM
FWIW, it appears the problem was simply a case of me using the wrong event handler....substituting onKeyPress with onKeyUp in my previous code also does the trick:


<HTML>
<HEAD>
<TITLE></TITLE>

<script type="text/javascript">
<!--

function submitenter(e)
{
if (e.shiftKey && e.keyCode == 13)
{
return true;
}
else if (e.keyCode == 13)
{
document.add_line.messagetext.value = "";
document.add_line.messagetext.focus();
return false;
}
else
{
return true;
}
}

-->
</script>

</HEAD>

<BODY>

<form name='add_line'>
<p>Type in a message:</p>
<textarea name='messagetext' onKeyUp='submitenter(event);'></textarea>
</form>

</BODY>
</HTML>

But I'm still not entirely sure why onKeyPress doesn;t work? The definitions of the events are here: http://www.quirksmode.org/js/events_compinfo.html#keys

Anyone know why onKeyPress was causing me problems?

Thanks.

_Aerospace_Eng_
08-05-2006, 05:49 AM
It seems the new line doesn't come until after the enter key is up rather than on keypress.

glenngv
08-06-2006, 12:53 AM
Why not simply use a textbox instead of textarea to avoid any scripting at all?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum