Javascript automatic save input txt value to div

I looking for some help to solve my problem.

I need to write JS function witch automatically save value of input field into div (with no button).

When value ends with "enter" key it save it to div, clear input field and set cursor in it again to put another value.

If anybody understood my english I would be appreciate for some tips:)

1 answer

  • answered 2018-01-16 20:07 Devan Buggay

    Here is a very simple example to get you started.

    const formElement = document.getElementById("form");
    const inputElement = document.getElementById("input");
    const outputElement = document.getElementById("output");
    
    formElement.addEventListener("submit", function (e) {
    
      outputElement.append(inputElement.value);
      inputElement.value = "";
    
      e.preventDefault();
    }, false);
    <form id="form">
      <input id="input"></input>
    </form>
    
    <div id="output">
    </div>