views:

340

answers:

2

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
+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
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
Try it with timeout. I've fixed the answer. It should help.
nemisj
thx, it works..
jeff