By pressing 'Enter' on focused <input type="text">
you trigger 'click' event on the first positioned element: <button>
or <input type="submit">
. If you press 'Enter' in <textarea>
, you just make a new text line.
See the example here.
Your code prevents to make a new text line in <textarea>
, so you have to catch key press only for <input type="text">
.
But why do you need to press Enter
in text field? If you want to submit form by pressing 'Enter', but the <button>
must stay the first in the layout, just play with the markup: put the <input type="submit">
code before the <button>
and use CSS to save the layout you need.
Catching 'Enter' and saving markup:
$('input[type="text"]').keypress(function (e) {
var code = e.keyCode || e.which;
if (code === 13)
e.preventDefault();
$("form").submit(); /*add this, if you want to submit form by pressing `Enter`*/
});