2

I want to focus an input field without selecting the text in it. I want cursor to be on the end and ready to add more text.

This is my input field:

<input type="text" name="change" value="<?php echo $row[4]; ?>" autofocus>

P.S - autofocus select the text.

nikitz
  • 1,051
  • 2
  • 12
  • 35
  • Do you allow jQuery solution? – Felix Feb 26 '14 at 16:37
  • 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) – Josh Durham Feb 26 '14 at 16:39

3 Answers3

3

If you know jQuery, then you can do like this:

var input = $('input[name="change"'),
    strLen = input.val().length;
input.val(input.val());
input.focus();
input[0].setSelectionRange(strLen, strLen);

You can find out more information about Input.setSelectionRange from MDN docs

Fiddle Demo

Felix
  • 37,892
  • 8
  • 43
  • 55
1

Try using this concept: http://jsfiddle.net/Lc45u/1/

<input id="inp1" value="abcdefg" /> 
<input type="button" onclick="document.getElementById('inp1').focus();document.getElementById('inp1').value = document.getElementById('inp1').value;" value="FOCUS" />
LcSalazar
  • 16,524
  • 3
  • 37
  • 69
1

I found a solution for that:

<input type="text" name="change" value="<?php echo $row[4]; ?>" autofocus onfocus="this.value = this.value;">

Thanks to everyone :)

nikitz
  • 1,051
  • 2
  • 12
  • 35