/    Sign up×
Community /Pin to ProfileBookmark

Disable enter button

I am trying to disable the enter button on my form and have it go to the next field instead of submitting the form. Here is what I have so far.

[code]

function enter(nextfield)
{
if(window.event && window.event.keyCode == 13) {
nextfield.focus();
return false; }
else
return true;
}
[/code]

and here is my text box

[code]
<td><input type=”text” id=”txtMfgName” name=”txtMfgName” value=”<%=rs(“MfgName”)%>” onkeyup=”enter(‘txtCustItem’)” style=”font-family: Verdana; font-size: 8pt” size=”20″></td>
[/code]

However, pressing enter just submits the form. Any ideas?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMay 04.2007 — Use onkeydown instead of onkeyup. By then it is too late, the form has already been submitted.

Are you aware that your code will only work on IE?
Copy linkTweet thisAlerts:
@dzirkelbauthorMay 04.2007 — I am in a controlled environment whre everyone uses IE, so thats ok.

Chaning it to onkeydown has the same results, submits form.
Copy linkTweet thisAlerts:
@Logic_AliMay 04.2007 — 
<i>
</i>onkeyup="enter('txtCustItem')"
[/QUOTE]
You're not capturing the return value:

Try:[CODE]onkeyup="return enter('txtCustItem')"[/CODE]
Copy linkTweet thisAlerts:
@dzirkelbauthorMay 04.2007 — adding the return produces same results
Copy linkTweet thisAlerts:
@dzirkelbauthorMay 04.2007 — I added an alert just to see if the function is firing off, and it is not...here is what I have for updated code:

<i>
</i>function enter(nextfield)
{
alert("hello")
if(window.event &amp;&amp; window.event.keyCode == 13) {
nextfield.focus();
return false; }
else
return true;
}

&lt;td&gt;&lt;input type="text" id="txtMfgName" name="txtMfgName" value="&lt;%=rs("MfgName")%&gt;" onkedown="return enter('txtCustItem')" style="font-family: Verdana; font-size: 8pt" size="20"&gt;&lt;/td&gt;


hitting any keys does not produce the alert, enter key submits the form
Copy linkTweet thisAlerts:
@Logic_AliMay 04.2007 — <i>
</i>&lt;td&gt;&lt;input type="text" id="txtMfgName" name="txtMfgName" value="&lt;%=rs("MfgName")%&gt;" [B]onkedown[/B]="return enter('txtCustItem')" style="font-family: Verdana; font-size: 8pt" size="20"&gt;&lt;/td&gt;


hitting any keys does not produce the alert, enter key submits the form[/QUOTE]
Assuming [I]txtCustItem[/I] is the name of another element in the same form, write it like this:[CODE]onkeyup="return enter(txtCustItem)"[/CODE]
Within the scope a form, its elements are referenced as variables not strings.

Your original code must have caused an indicated error, which for a paid developer might cause concern.
Copy linkTweet thisAlerts:
@gil_davisMay 04.2007 — I think you are still using the wrong event. You have to cancel the down event or it will propagate to keypress. Cancelling keyup will not stop keypress.onkeydown = "return enter(txtCustItem)"
Copy linkTweet thisAlerts:
@dzirkelbauthorMay 04.2007 — I had a typo...I had onkedown instead of onkeydown...it works now, thanks!
×

Success!

Help @dzirkelb 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,
)...