Solved

Focus at end of textarea

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

705 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

12 Experts available now in Live!

Get 1:1 Help Now