Solved

Focus at end of textarea

Posted on 2004-09-23
4
3,441 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now