As you are using Bootstrap, the example below uses jQuery.
var ENTER_KEY = 13;
$('textarea').on('keypress', function(event) {
var char = event.which || event.keyCode;
if (char == ENTER_KEY) {
event.preventDefault();
$(this).parent('form').submit();
}
})
I didn’t put as snippet because Stackoverflow blocks, but you can see it working on Jsfiddle.
If you want to keep the line break when the user presses SHIFT + ENTER can make the check with mouveEvent#shiftKey()
returning true
if the shift is pressed:
var ENTER_KEY = 13;
$('textarea').on('keypress', function(event){
var char = event.which || event.keyCode;
if(char == ENTER_KEY){
event.preventDefault();
if(event.shiftKey){
$(this).val($(this).val() + '\n'); // shift + enter = Quebra linha
return;
}
$(this).parent('form').submit(); // somente o Enter = Envia o form
}
});
Online example on Jsfiddle.
API:
Do a Javascript for this.
– PauloHDSousa
If you don’t want to break text, use a
<input type="text">
!!!– utluiz
Naldson, the answer you marked as correct is actually incorrect. I suggest you review your choice. Think of the people who will suffer later trying to use this code that doesn’t work.
– utluiz