Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to make input field in edit mode(insert mode)

Tags:

javascript

How is it possible to make a input field editable in javascript. I mean onFocus putting it in insert mode so that values can be overwritten. Any suggestions ???

like image 924
user234194 Avatar asked Apr 01 '10 14:04

user234194


1 Answers

This should work in modern browsers (also on mobile):

var input = document.querySelector('input'); // or a textarea
input.addEventListener('keypress', function(){
    var s = this.selectionStart;
    this.value = this.value.substr(0, s) + this.value.substr(s + 1);
    this.selectionEnd = s;
}, false);

jsfiddle

Note: This is a basic form of insert functionality so some default functionality like CTRL+Z may break.

like image 156
A1rPun Avatar answered Oct 25 '22 02:10

A1rPun