Text Overwriting

Automatic text overwriting while typing

by Génesis García Morilla

HTML

<input type="text">

JavaScript

var $input = document.querySelector("input");

$input.addEventListener("keypress", function(e) {
  var cursor_pos = e.currentTarget.selectionStart;
  
  if (!e.charCode) return;
  
  this.value = this.value.slice(0, cursor_pos) + this.value.slice(cursor_pos + 1);
  e.currentTarget.selectionStart = e.currentTarget.selectionEnd = cursor_pos;
});