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

前端 未结 4 1807
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 01:19

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 ???

4条回答
  •  生来不讨喜
    2021-01-12 01:44

    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.

提交回复
热议问题