tags:

views:

32

answers:

1

I want to position a cursor after an inserted word (test), in textarea.
The insertion of the word can be in any position in textarea.
(Internet Explorer)
This is my script:

 document.activeElement.focus();

 document.selection.createRange().text = "test";

 var sel = document.selection.createRange();
 sel.moveStart('character', -document.activeElement.value.length);
 var cursorPos = sel.text.length;

 var range = this.textarea.createTextRange();
 range.collapse(true);
 range.moveEnd('character', cursorPos);
 range.moveStart('character', cursorPos);
 range.select();
A: 

This will do it (in Internet Explorer only, you'll need a totally different approach for other browsers):

document.activeElement.focus();
var sel = document.selection.createRange();
sel.text = "test";
sel.collapse(false);
sel.select();
Tim Down
This is good, but i have a sub-question: how can I move cursor X steps from current position? I need to use moveEnd and moveStart?
shivesh
skip the last comment, i will accept this answer and open a new question.
shivesh