I have several input fields with default values. I defined tabindexes to jump from field to field with the tab-button. The problem is that when I get to a field the whole text gets selected, and it is replaced if I start to type. I would like to set the cursor position to the end of the pre-filled text. How can I do this ?
Asked
Active
Viewed 5,653 times
2 Answers
1
By defining onfocus event on every node. And setting caret position to the end. Because of the default focus/click behavior, setting cursor should be deferred.
function setCursorAtEnd(node){
setTimeout(function(){
setCursor(node,node.value.length);
},10);
}
<input onfocus="setCursorAtEnd(this)" value="bla"/>
The setCursor
function you can find in this question: Set cursor at a length of 14 onfocus of a textbox
-
the function is only working if i go to the field with the mouse. if i switch with the tab button between the fields i get no effect :( – jeff Dec 16 '09 at 10:44
-
Try it with timeout. I've fixed the answer. It should help. – nemisj Dec 16 '09 at 13:07
-1
I believe you cannot set the cursor position with js, but you could hook a function to reset the prefilled text after the first key press.

jbochi
- 28,816
- 16
- 73
- 90