javascript - don't can use Enter key from textarea -
in project need diable enter key textbox , because don't want post page when enter key button . use code disable enter key :
$(document).keypress( function (event) { if (event.which == '13') { event.preventdefault(); } });
its work fine , when add textarea in page , cant enter key break line , because enter key disabled .
how can enable enter key textarea?
i don't know if recommended attach global keypress
handler that. regardless, easiest way out be
$(document).on('keypress', function (event) { if (event.which == '13' && event.target.tagname != 'textarea') { event.preventdefault(); } });
in above, checking tagname
of event.target
see if element in enter
occured textarea
or not
however, recommend approach
$('form').on('keypress', 'form', function (event) { if (event.which == '13') { event.preventdefault(); } });
this target all input
elements , not textarea
elements,
Comments
Post a Comment