Selecting text in input field

By | 2012/11/16
var input = document.getElementById("some-input");

Full selection:

input.select();

Partial selection (selects second letter):

input.setSelectionRange(1, 2);

Putting carret in specific places:

input.setSelectionRange(0, 0);//Begining of the input field
input.setSelectionRange(1, 1);//After first letter

To get beginning and end points of the selected text use:

input.getSelectionStart
input.getSelectionStart

To set beginning and end points of the selected text use:

input.getSelectionStart = 3;
input.getSelectionStart = 5;

Suppored in all browsers. (IE9+)