/    Sign up×
Community /Pin to ProfileBookmark

how to skip document.onkeypress if its coming from textarea

Hi ,

I am building a web application using jsp and javascript

there is a requirement whenever use press “Enter” Button the form will be submitted so i use this method below

[CODE]document.onkeypress = submitViaEnter;

function submitViaEnter(evt) {
evt = (evt) ? evt : event;
var target = (evt.target) ? evt.target : evt.srcElement;
var form = target.form;
var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
if (charCode == 13 || charCode == 3) {
//submit the form

}

}
[/CODE]

but there is a problem for textarea that should be excluded in this case.

how do i do that?
one way i can think of is whenever javascript there is a “Enter” event can we check if the user press “Enter” Button from inside the textarea so we dont submit the form..

or is there any other way?

Thanks.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 14.2008 — check for the [B]nodeName[/B] DOM property
<i>
</i>function submitViaEnter(evt) {
evt = (evt) ? evt : event;
var target = (evt.target) ? evt.target : evt.srcElement;
[COLOR="Blue"] if(target.nodeName=='TEXTAREA'){return}[/COLOR]
var form = target.form;
var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
if (charCode == 13 || charCode == 3) {
//submit the form

<i> </i>}

}
×

Success!

Help @just_a_kid spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...