Log in

View Full Version : Textarea cursor position


benn11
Jan 2, 2008, 05:01 AM
I have a textarea in an asp page and I have an attribute to insert * symbol in the textarea upon a click in the textarea. Using "<textarea name="text" onfocus="if(this.value=='') this.value='*';">"

The problem is that the cursor returns to the start position but I want the cursor to remain in the position after the * symbol is automatically inserted. How can this be done?

jstrike
Jan 16, 2008, 09:20 AM
This should work:


<textarea name="myTextArea" onClick="placeText(this,'*')"></textArea>

<script>
function placeText(txtArea, txt) {
if(txtArea.value!="") return;

txtArea.value=txt;
//FF will automatically put the cursor at the end so skip this IE specific part...
if(navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
var range = txtArea.createTextRange();
range.moveStart('character', 1);
range.select();
}
}
</script>