i have several inputfields with some defoult values. i have defined tabindexes to jump with tab-button from field to field. the problem is if i jump to a field the fieldtext become selected and if i start to type it will be replaced. i would like to set the cursor position et the end of the prefilled text. how can i do it
A:
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
2009-12-15 18:12:03
+1
A:
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: http://stackoverflow.com/questions/1865563/set-cursor-at-a-length-of-14-onfocus-of-a-textbox/1867393#1867393
nemisj
2009-12-15 18:13:44
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
2009-12-16 10:44:45
Try it with timeout. I've fixed the answer. It should help.
nemisj
2009-12-16 13:07:56
thx, it works..
jeff
2009-12-17 11:36:03