Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textarea Cursor Possition

Status
Not open for further replies.

DaveShaw

Programmer
Jul 9, 2001
35
0
0
GB
I am trying to set focus on a textarea and put the sursor at the end of the text.

I can set focus but cannot got to the end. Is this possible?


Thanx Dave Shaw!
 
I pulled this from a Google group thread. Looks like it should work:

Here is a more complete example wich allows ie4/5 to insert previously
selected text into a textarea by clicking at the desired insertion
point. Look at the end of the page:
Code:
<HTML>
<HEAD>
<TITLE>

</TITLE>
<STYLE>

</STYLE>
<SCRIPT>
function setCaretToStart (el) {
  if (el.createTextRange) {
    var v = el.value;
    var r = el.createTextRange();
    r.moveEnd('character', -v.length);
    r.select();
  }
}
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);
}
function insertAtStart (el, txt) {
  el.value = txt + el.value;
  setCaretToStart (el);
}
function insertOnClick (el, txt) {
  var r = document.selection.createRange();
  r.text = txt;
}
function insertTag (el, sel) {
  var v = sel.options[sel.selectedIndex].value;
  insertOnClick(el, v);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top