Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,797
    Thanks
    19
    Thanked 156 Times in 147 Posts

    set focus inside of textarea

    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:

    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.
    Last edited by chump2877; 08-03-2006 at 09:38 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ireland
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Maybe browser recogonises line as 13 (carriage return) and 10 (line feed)?
    test 10 in your script.

    just a wild guess!

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,797
    Thanks
    19
    Thanked 156 Times in 147 Posts
    Quote Originally Posted by tom123
    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?
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,797
    Thanks
    19
    Thanked 156 Times in 147 Posts
    No one knows the answer to this question?? It seems like it should be a no-brainer for the right person....thanks.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    Code:
    <!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>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,797
    Thanks
    19
    Thanked 156 Times in 147 Posts
    Quote Originally Posted by _Aerospace_Eng_
    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!
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #7
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,797
    Thanks
    19
    Thanked 156 Times in 147 Posts
    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:

    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' 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.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    It seems the new line doesn't come until after the enter key is up rather than on keypress.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why not simply use a textbox instead of textarea to avoid any scripting at all?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •