javascript - Prevent user hitting Enter except Textarea -
i have complex form has multiple stages many different text fields , text areas. environment used within used barcodes hit enter default, , choice scan multitude of products using them turning enter function off has become no-go.
i using form wizard script handles client-side validation during input stage. script interrupted enter key being hit during filling out form , refuses submit until page refreshed.
<a href="javascript:;" class="btn green button-submit">submit <i class="m-icon-swapright m-icon-white"></i></a>
i have following code works prevent enter on form , allows form submit when link above clicked.
$(window).keydown(function (event) { if (event.keycode == 13) { event.preventdefault(); return false; } });
however prevents enter being used within textarea. did bit of research , tried using is()
operator jquery
$(document).ready(function () { $(window).keydown(function (event) { if (event.keycode == 13 && !event.is("textarea")) { event.preventdefault(); return false; } }); });
this doesn't work, fails prevent enter key in inputs , stalls form submitting did prior.
finally javascript handles submitting form if validation passes on form
$('#form_wizard_1 .button-submit').click(function () { // can put more onsubmit processing here document.getelementbyid('submit_form').submit(); }).hide();
can suggest how can prevent enter key on inputs except textareas. don't pretend javascript developer, although trying learn go. following articles have read , either attempted adapt code or failed understand how apply me in correct manner:
prevent users submitting form hitting enter
prevent users submitting form hitting enter #2
disable enter key on page, not in textarea
*in regards last link, need global resolution automatically prevents on textareas may exist within form.
as always, thank assistance.
use !$(event.target).is("textarea")
jsbin demo
use event.which
(instead of event.keycode
; jquery normalizes xbrowser)
$(function() { // dom ready $(window).keydown(function(event){ if(event.which==13 && !$(event.target).is("textarea")) { event.preventdefault(); console.log("enter prevented"); return false; }else{ // test console.log("enter in textarea"); } }); });
Comments
Post a Comment