?
Solved

Focus at end of textarea

Posted on 2004-09-23
4
Medium Priority
?
3,485 Views
Last Modified: 2008-02-01
In my window_onload function, I am setting the focus on a textarea in my form.  It will show text previously entered in the textarea.  However it puts the cursor at the beginning of the text.

Is there a way I can make the cursor start at the end of what has been typed in?

Here is the function I'm currently using: document.getElementById("comment").focus();
0
Comment
Question by:ags00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 12136571
The trick is to append a space so that the focus goes down to the end of the textarea...

eg:
document.getElementById("comment").focus();
document.getElementById("comment").value = document.getElementById("comment").value  + " ";


PS: You can extend this technique to read the last char of the textarea and append that char by stripping the last char !



~Hemanth
0
 
LVL 24

Accepted Solution

by:
HemanthaKumar earned 300 total points
ID: 12136580
or simply add  "" (null) to the end..
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12136585
I googled a while and found a function which you can use to put the cursor at the end:
<script language="JavaScript" type="text/JavaScript">
function MoveToEnd(Element){

if ( Element.createTextRange )
Element.createTextRange().text += "";
else if ( Element.insertionPoint )
Element.insertionPoint = Element.text.length;
}
</script>

You might want to try to use a function like:
<script type="text/javascript">
function focusById(elemid)
{
  elem = document.getElementById(elemid);
  if(elem)
  {
    elem.focus();
    MoveToEnd(elem);
    elem.focus();
  }
}

I tested it on IE6.028 using a simple html:
<body onload="focusById('mygoto');">
<form>
 <input id="mygoto" value="soup" />
</form>
</body>

And it worked fine; but i haven't tested it on other clients though.

good luck

-r-
0
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 12136587
One drawback is the cursor moves to the end.. but textarea doesn't scroll up. TO achieve that use doScroll method to scroll down the pages
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question