10

I want to put autofocus on a html textbox such that the cursor points to the end of some text already present in the textbox. I know what the autofocus attribute does. And I have also seen put cursor at end of text input's value which answers how to put cursor at the end, But the textbox is not autofocussed upon page reload. How can I do both - autofocus the textbox upon page reload and put cursor to end of text ?

Code : 

 <textarea id="txtArea" style="width:106%; height:100px" align="center" name="task1" onkeypress="onTestChange();" onfocus="this.value = this.value;" autofocus ><?php echo $_GET["task"];?></textarea>

I want to put Cursor after the text echoed. It is not the value of the textarea.

Also the textbox I referred to is Textarea.

Community
  • 1
  • 1
Stack Man
  • 579
  • 2
  • 6
  • 11

6 Answers6

19

Please use this code and run in your browser

$(document).ready(function() {
  
    var input = $("#test");
    var len = input.val().length;
    input[0].focus();
    input[0].setSelectionRange(len, len);

});
<input type="text" id="test" autofocus value="value text" />
Avinash Antala
  • 641
  • 5
  • 10
3

try to run this simple code and see u will notice that first text box will be focused and not the second

<input type="text" autofocus value="value text" onfocus="this.value = this.value;"/>
<input type="text" autofocus value="value text" onfocus="this.value = this.value;"/>
Vitorino fernandes
  • 15,794
  • 3
  • 20
  • 39
kki3908050
  • 165
  • 2
  • 9
2

Something like this. Focus it, and set the value again.

<input type="text" value="this holds a value" id="element"/>

<script>
$(window).ready(function(){
   $("#element").focus(); 
   $("#element").val($("#element").val());
});
</script>
philz
  • 1,012
  • 6
  • 11
2
$( document ).ready(function() {

 var t =$("#txtID");    

 t.focus().val(t.val());

});
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
AKHIL K A
  • 59
  • 6
1

Check chris G's answer on this question. Call function after you have set the focus to the textbox:

   function SetCaretAtEnd(elem) {
            var elemLen = elem.value.length;
            // For IE Only
            if (document.selection) {
                // Set focus
                elem.focus();
                // Use IE Ranges
                var oSel = document.selection.createRange();
                // Reset position to 0 & then set at end
                oSel.moveStart('character', -elemLen);
                oSel.moveStart('character', elemLen);
                oSel.moveEnd('character', 0);
                oSel.select();
            }
            else if (elem.selectionStart || elem.selectionStart == '0') {
                // Firefox/Chrome
                elem.selectionStart = elemLen;
                elem.selectionEnd = elemLen;
                elem.focus();
            } // if
        } // SetCaretAtEnd() 
Community
  • 1
  • 1
TheProvost
  • 1,832
  • 2
  • 16
  • 41
0

The answer to your link already auto focused on page load.

<input type="text" autofocus value="value text" onfocus="this.value = this.value;"/>

JS Fiddle

Eliel
  • 164
  • 6