On popup of a page, focus cursor at the end of a textfield

1
Upon opening a form and having only one field open for modification, my cursor is placed in that field, which is what we want. But this field is prefilled with a couple of characters, so the user only has to enter two more characters. Unfortunately, the user has to press β†’ to make the characters get deselected and to get to see this, with the cursor blinking behind the 4003: Is there any way to make this happen upon opening the form? So without the user having to click β†’ first?
asked
2 answers
1

Rough cut (global)

if(typeof(window.hdlFocusSelEnd)=='undefined'){
    var hdlFocusSelEnd=function(event) {
        var type = event.target.nodeName.toLowerCase();
        if(type=='input'||type=='textarea'){
            window.setTimeout(function(){try{
            event.target.selectionStart=event.target.selectionEnd=event.target.value.length;
    		}catch(e){console.error(e);}},100);
        }
    };
    document.body.addEventListener('focus',hdlFocusSelEnd,true); //normal browsers  
    document.body.onfocusin=hdlFocusSelEnd; //fake browser (i.e. ie)
}else{
    console.error('hdlFocusSelEnd already attached')
}

Further filtering can be applied as to parent divs or element class etc if the global behavior is not desired

answered
1

May be you can do this with Javascript. See stackoverflow here: https://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element

Regards,

Ronald

 

answered