43

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 />

3
  • 2
    onfocus="this.value = this.value;" Commented Jul 22, 2013 at 5: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. Commented Jul 22, 2013 at 5:57
  • 2
    possible duplicate of Use JavaScript to place cursor at end of text in text input element Commented Jul 30, 2015 at 12:53

6 Answers 6

49

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" />

Sign up to request clarification or add additional context in comments.

2 Comments

@vol7ron "chenosaurus's method" :D
I'm (quite new to) Spring boot and using it. Seemed I needed to use this method to get desired effect.
41

This works for me

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

4 Comments

Just to update, this either no longer works in FF, or never did. In the current version of FF, it selects the entire text, it does not place cursor after.
I found this answer helpful and works just fine cross multiple browsers: stackoverflow.com/a/19568146/936468
Does not work in FF (2022)
It doesn't work in Chrome (anymore) either.
9

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);"

1 Comment

I can confirm this works perfectly. I am using Brave (based on chromium)
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

1 Comment

It's a little bit of an overkill to use a thirdparty library for such an easy task
4

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.

1 Comment

works perfect in FF and Chrome
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.