faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

71 of 98 people (72%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I move text cursor in a text input field?

Apr 7th, 2008 22:31
ha mo, Justin Hansen, Lzan Lind, Martin Honnen ([email protected])


So here is how to position the caret end the end of a text field/text
area with IE4/5:
<SCRIPT>
function setCaretToEnd (el) {
  if (el.createTextRange) {
    var v = el.value;
    var r = el.createTextRange();
    r.moveStart('character', v.length);
    r.select();
  }
}
function insertAtEnd (el, txt) {
  el.value += txt;
  setCaretToEnd (el);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE="text" NAME="aText" SIZE="40">
<BR>
<INPUT TYPE="text" NAME="a2ndText" VALUE="Kibology for all.">
<INPUT TYPE="button" VALUE="insert and move to end"
       ONCLICK="insertAtEnd (this.form.aText,
this.form.a2ndText.value);"
>
<BR>
<INPUT TYPE="text" NAME="a3rdText" VALUE="All for Kibology.">
<INPUT TYPE="button" VALUE="insert and move to end"
       ONCLICK="insertAtEnd (this.form.aTextArea,
this.form.a3rdText.value);"
>
<BR>
<TEXTAREA NAME="aTextArea" ROWS="5" COLS="20" WRAP="soft"></TEXTAREA>
</FORM>
No need for a java applet for those browsers it seems.
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com