40

So, currently I have a text-input-field with a value that is also autofocused.

On page load, the value is selected / highlighted. Is there a way I can put the cursor at the end of the value text instead of highlighting it in javascript or CSS?

Here is a js fiddle where the autofocused text's value is highlighted: http://jsfiddle.net/TaGL5/

Here is the HTML code: <input type="text" value="value text" autofocus />

Django Johnson
  • 1,383
  • 3
  • 21
  • 40
  • 2
    onfocus="this.value = this.value;" – Cԃաԃ Jul 22 '13 at 05:56
  • onfocus "this.value = this.value" will assign existing value to textbox, it's like you typed that text and so you have cursor at end. – VishalDevgire Jul 22 '13 at 05:57
  • 1
    possible duplicate of [Use JavaScript to place cursor at end of text in text input element](http://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element) – Gary Jul 30 '15 at 12:53

6 Answers6

44

Upgrade to @harsha's answer

I found that to make solution work with Firefox, we need temporary reset value to "not-equal of value", then set it back

<input type="text" autofocus value="value text" onfocus="var temp_value=this.value; this.value=''; this.value=temp_value" />
Souleste
  • 1,887
  • 1
  • 12
  • 39
J_z
  • 993
  • 1
  • 9
  • 18
40

This works for me

<input type="text" autofocus value="value text" onfocus="this.value = this.value;"/>
Gary
  • 13,303
  • 18
  • 49
  • 71
Harsha Venkataramu
  • 2,887
  • 1
  • 34
  • 58
8

Use Jquery for this:

$(function() {
      var input = $("#txt1");
    var len = input.val().length;
    input[0].focus();
    input[0].setSelectionRange(len, len);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<input type="text" id="txt1" value="Lorem" style="width:400px;" />

But some browsers don't support enter code here property, in which case use this:

$(document).ready(function(){
    $("#search").focus(function(){
        if (this.setSelectionRange)
        {
        var len = $(this).val().length;
        this.setSelectionRange(len, len);
        }
        else
        {
        $(this).val($(this).val());
        }
   
});

$("#search").focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<input id="search" type="text" value="mycurrtext" size="30" name="search" />

This question already exist on stackoverflow:

Reference1

Reference2

Community
  • 1
  • 1
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
  • 3
    It's a little bit of an overkill to use a thirdparty library for such an easy task – shi Oct 28 '16 at 13:39
8

Use this, its nice work for me...

<input type="text" name="txt" value="value text" autofocus="" onfocus="this.setSelectionRange(this.value.length,this.value.length);">

OR add this line to your input element

onfocus="this.setSelectionRange(this.value.length,this.value.length);"
Obaidul Haque
  • 916
  • 11
  • 18
3

You can add this parameter to your text input field:

onmouseover="this.setSelectionRange(this.value.length,this.value.length);"
onfocus="this.setSelectionRange(this.value.length,this.value.length);"

NB: Works well under Chromium in 2017.

Obaidul Haque
  • 916
  • 11
  • 18
0

Select element by class or id, then focus, and then re-insert value.

<input type="text" class="element-to-autofocus" value="Some existed text" />
<script>
  temp_el = document.querySelector('.element-to-autofocus');
  temp_el.focus();
  temp_value = temp_el.value;
  temp_el.value = '';
  temp_el.value = temp_value;
</script>
Anindya Dey
  • 825
  • 1
  • 8
  • 18