Block Enter Key of Textarea Elements
by Kasil
HTML
<form action="">
<textarea cols="26" rows="1" maxlength="26" placeholder="Write your answer here." name="G17"></textarea><br>
<textarea cols="26" rows="5" maxlength="26" placeholder="Write your answer here." name="G18"></textarea>
<br>
<input type="submit"/>
</form>
<script>
var listOfTextareas = document.getElementsByTagName("textarea");
Array.from(listOfTextareas).forEach(applyListenerToTextarea);
function applyListenerToTextarea(textareaElement)
{
textareaElement.addEventListener('keypress', function (e) {
if (e.keyCode == 13 && !e.shiftKey)
{
// prevent default behavior
e.preventDefault();
alert("The 'enter' key is not allowed.");
return false;
}
});
}
</script>
<hr>
<textarea cols="26" rows="5" maxlength="26" placeholder="Will not work if JavaScript is placed ahead of the textareas." name="G19"></textarea>