I am developing a system in
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>