38

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,295
  • 3
  • 20
  • 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

5 Answers5

40

This works for me

<input type="text" autofocus value="value text" onfocus="this.value = this.value;"/>
Gary
  • 12,206
  • 16
  • 46
  • 70
Harsha Venkatram
  • 2,782
  • 1
  • 31
  • 57
36

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,721
  • 1
  • 9
  • 37
J_z
  • 843
  • 1
  • 9
  • 18
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
  • 7,819
  • 9
  • 46
  • 75
  • 2
    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
7

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
  • 839
  • 10
  • 17
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
  • 839
  • 10
  • 17