Search code examples
javascripttextareakeycode

javascript textarea clear value on enter key


So, I was trying to make a textarea which when I press enter, it should submit the value then clear it.

The submit part was fine, however, instead of going back to the beginning of the textbox, it has a second line.

document.getElementById("content").blur();
document.getElementById("content").value = "";
document.getElementById("content").value.replace(/[\n]/i, '');
document.getElementById("content").focus();

I tried that to clear my textarea with the id = content, it still does not work....


Solution

  • You should return false on the keydown event if the actual press was enter. That way the original event (new line to the textbox) won't be fired.