Search code examples
javascripttextboxdouble-clicktextchanged

Javascript: How to implement the "enter/return key" to save a value?


Sorry, I am not really good with JS.

The code is essentially the user double clicks on the text, textbox appears, changes text, and saves a new value. However, I want the user to be able to also click enter to save the new value.

In addition, if possible, to have a dedicated "Save" button to save the new value and "discard" to keep the old value.

Also, if I double click many times, the text appears as "(input type="text")". Is there a way to remove that?

Please help if you can.

The HTML + JS code

<html>
<head>
    <script type="text/javascript">
        window.onload = function() {
            var elements = getElementsByClassName('text-edit', '*', document);
            for(var i = 0; i < elements.length; i++) {
                elements[i].ondblclick = function() {
                    this.setAttribute('oldText', this.innerHTML); // not actually required. I use this just in case you want to cancel and set the original text back.
                    var textBox = document.createElement('INPUT');
                    textBox.setAttribute('type', 'text');
                    textBox.value = this.innerHTML;
                    textBox.onblur = function() {
                        var newValue = this.value;
                        this.parentNode.innerHTML = newValue;
                    }

                    this.innerHTML = '';

                    this.appendChild(textBox);
                }
            }(i);
        }

        function getElementsByClassName(className, tag, elm) {
            var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
            var tag = tag || "*";
            var elm = elm || document;
            var elements = (tag == "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag);
            var returnElements = [];
            var current;
            var length = elements.length;
            for(var i = 0; i < length; i++) {
                current = elements[i];
                if(testClass.test(current.className)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        }
    </script>
</head>
    <div><span class="text-edit">Some text</span></div>
</html> 

Solution

  • The snippet below allows you to modify the value of a textbox using save button or enter key and discarding any changes using cancel button.

    <!-- HTML -->
    <h1 id="editable">Lorem Ipsum</h1>
    
    // JavaScript
    window.onload = function(){
      var h1 = document.getElementById('editable');
      h1.onclick = function(){
        var old = this;
        var input = document.createElement("INPUT");
        input.type = "text";
        input.value = this.innerHTML;
        input.onkeyup = function(e){
          var code = (e.keyCode ? e.keyCode : e.which);
          if(code == 13) {
            old.innerHTML = input.value;
            input.parentNode.replaceChild(old, input);
            save.parentNode.removeChild(save);
            cancel.parentNode.removeChild(cancel);
          }      
        };    
        this.parentNode.replaceChild(input, this);
        var save = document.createElement("INPUT");
        save.type = "button";
        save.value = "Save";
        (function(old, input){
          save.onclick = function(){
            old.innerHTML = input.value;
            input.parentNode.replaceChild(old, input);
            this.parentNode.removeChild(this);
            cancel.parentNode.removeChild(cancel);
          };
        })(old, input);
        input.parentNode.insertBefore(save, input.nextSibling);
        var cancel = document.createElement("INPUT");
        cancel.type = "button";
        cancel.value = "Cancel";
        (function(old, input){
          cancel.onclick = function(){
            input.parentNode.replaceChild(old, input);
            this.parentNode.removeChild(this);
            save.parentNode.removeChild(save);
          };
        })(old, input);
        input.parentNode.insertBefore(cancel, input.nextSibling);
      };
    };
    

    Working jsBin