Solved

Focus at end of textarea

Posted on 2004-09-23
4
3,472 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 75 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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 shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…
Suggested Courses

626 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