Show value of a text box by pressing Enter

-1

I am developing a system in

asked by DriversDigital 17.04.2017 в 16:55
source

1 answer

1

Simply intercept the event keypress and check if the key pressed was intro . You can do this through KeyboardEvent#code or KeyboardEvent#key . It used to be KeyboardEvent#keyCode but has been declared obsolete and will be removed from the standard , so in the near future will not exist in browsers. For now, should be used since not all browsers implement the new standard for code and key .

const output = document.getElementById('output');

function save(el, e) {
  if (e.key === 'Enter' || e.keyCode === 13) {
    output.textContent += el.value;
    el.value = '';
  }
}
<input onkeypress="save(this, event)"/>
<pre id="output"></pre>
    
answered by 17.04.2017 в 17:45