Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 693509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:45:40+00:00 2026-05-14T02:45:40+00:00

I’m seeing my Friend’s code here… <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN> <HTML>

  • 0

I’m seeing my Friend’s code here…

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Check action </TITLE>

<script>

    function detectEvent(){
    if(window.event.keyCode==13)
        {
            alert("you hit return!");
        }

    }
</script>


</HEAD>

<BODY>
    <form name="name1" onkeyup="detectEvent()" action="page2.html">
        <p> Field1
            <input type="text" id="text1"/>
        </p>
    </form>
</BODY>
</HTML>

and when he tried entering a value in the textbox and pressed enter, it did not call the detectEvent(). I said, it’ll always call onSubmit on enter button…..
and he surprised me,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Check action </TITLE>

<script>

    function detectEvent(){
    if(window.event.keyCode==13)
        {
            alert("you hit return!");
        }

    }
</script>


</HEAD>

<BODY>
    <form name="name1" onkeyup="detectEvent()" action="page2.html">
        <p> Field1
            <input type="text" id="text1"/>
        </p>
        <p> Field1
            <input type="text" id="text2"/>
        </p>
    </form>
</BODY>
</HTML>

Now press enter, The function gets called…..
Why so!?

Why onKeyUp not called on forms with just one field.!!! am i missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:45:40+00:00Added an answer on May 14, 2026 at 2:45 am

    The order of events is:

    keydown
    keypress
    submit
    keyup
    

    So by the time your keyup handler would have been called, the form has already started submitting. If the action of the form is a page2.html on the local filesystem, that’s going to navigate very quickly and probably move away from the page before keyup can be called; set the action to an external site, on the other hand, and keyup will have time to fire.

    Adding the second input field is a different issue: in this case the form is not submitted at all. It is a curious historical quirk that browsers will submit a form that has only one input and no submit button, but refuse to submit a form with more than one input, and no submit button. This goes back to the HTML 2.0 spec:

    When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.

    HTML 2.0 didn’t specify whether or not Enter should be accepted to submit a form in any other circumstances (the intention seems only to have been to duplicate the functionality of the ancient <isindex> element), but browsers of the day seem to have interpreted that to mean that Enter-submission shouldn’t happen where there are multiple fields, but then any submit button causes Enter-submission to happen anyway. IE and other later browsers copied this odd behaviour.

    Unrelated point: the reliance on window.event makes the code needlessly IE-only. For all other browsers, the event object is passed in as an argument to the event handler function. You can do onkeyup="detectEvent(event)" in the HTML and then use that argument in the detectEvent function, which works on both models because either the local argument event or the global window.event is used. But the usual approach would be to lose the event handler attribute and assign from JavaScript:

    <form action="page2.html" id="enterform">
        <p> Field1
            <input type="text" id="text1">
            <!-- Note: *no* trailing slash - the doctype says HTML4, not XHTML -->
        </p>
    </form>
    
    <script type="text/javascript">
        document.getElementById('enterform').onkeyup= function(event) {
            if (event===undefined) event= window.event; // for IE
            if (event.keyCode===13)
                alert('You pressed Enter');
        };
    </script>
    

    Having said all that… I’m generally suspicious of trapping the Enter key. I’m not quite sure what you’re trying to do, but if it’s the common case of changing the default button in a form this definitely isn’t the way to do it and will cause an assortment of problems. There is no good reason you should be trapping the Enter key to call .submit() on a form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.