Set input value and keep cursor position

npm install set-input-value
2 downloads in the last month


Set an input's value without losing the user's cursor position.


var set = require("set-input-value");
var input = document.getElementById("my-input");

// Uppercase the value
set(input, input.value.toUpperCase());

// Add a prefix and shift the cursor accordingly
var prefix = "A: ";
set(input, prefix + input.value, prefix.length);


set(input, value, shift)

Update the value of the given input element.

  • input: input element to update
  • value: new value
  • shift: amount to shift the previous cursor by (default: 0)

Browser Support

This module currently uses the input.setSelectionRange which is not supported in IE before version 9. A polyfill is possible - see


npm install set-input-value
npm loves you