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

  • Home
  • SEARCH
  • 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 8358259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:44:24+00:00 2026-06-09T10:44:24+00:00

I have a function that gets key value of keyboard. I want to know

  • 0

I have a function that gets key value of keyboard. I want to know why we need to use event in parameter

<form>
Char: <input type="text" id="char" size="15" /> Keycode: <input type="text" id="keycode" size="15" />
</form>

<script type="text/javascript">

var charfield=document.getElementById("char")
charfield.onkeydown=function(event){
//var e=window.event || e
document.getElementById("keycode").value=event.keyCode
}

</script>
  • 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-06-09T10:44:28+00:00Added an answer on June 9, 2026 at 10:44 am

    JS is EVENT DRIVEN, not having access to the event object would be racing in F1 with your bike: you can see the race (cars === events) flying by, but you can’t get to them… (sorry for the dreadful analogy, but you get the point).
    To increase/enable more interactivity, you have to know what the client does, so you can respond accordingly. If the user clicks, you might want to know where on the page he clicked, for example. All this and more can be found in the event object. Not having that, is like going to war, unarmed, blindfolded with your hand tied behind your back.

    Think about it: you wouldn’t ask the same question in any other programming language where events are crucial (like C++, C#, Java, ActionScript…), anything that gets its input directly from the user, really.

    Well, both answers (AlphaMale and chiborg) have told you what the event object is for. The line var e= window.event || e; won’t work in your case, though because you’ve got the default operator jumbled up. Try this:

    charfield.onkeydown = function (e)//call parameter e, rather then event
    {
        e = e || window.event;//no var, as a parameter, it's already declared, won't set a global
        var key = e.keyCode || e.which;//another default operator here... thx to IE
        //and so on
    
        if (e.preventDefault)
        {
            e.preventDefault();
            e.stopPropagation();
            return false;
        }
        e.returnValue = false;
        e.cancelBubble = true;
    };
    

    So, here’s a small list of methods you can (and often will) use on the event object.

    e = e || window.event; is the same as e = e !== undefined ? e : window.event; Since IE doesn’t pass the event object to the handler, you’ll have to get it yourself (it’s a property of the global object, hence window.). Why this didn’t work for you is because you gave the JS engine the choice between window.event (IE only) and e, which you were declaring, the actual event object was stored in the event parameter. You should have written event = event || window.event;

    The second line var key = e.keyCode || e.which; gets the key code of what key was pressed, again IE and W3C have agreed to disagree on what this property should be called. There is, in fact only 1 property that they’ve always agreed on: type. Some more properties here

    Lastly, I’ve invoked a couple of methods that allow you to prevent/block the event. Suppose you’re asking for a telephone number and the user starts typing text. Using the any of the onkey* events you can pick up on this, and use preventDefault, stopPropagation methods to stop the input from being accepted. (there’s a little more to it than that, but I’m not going into that right now).
    again most browsers have these methods for that, but IE has a different event model all together read about it at length here And with that comes the different take on stopping an event in its tracks for IE: returnValue and cancelBubble properties, the latter being particularly annoying, because you set it to true if you want something not to happen… but that’s just my opinion.

    There’s a lot more you can do with the event parameter, but in a nutshell -again- JS is EVENT DRIVEN, not having access to the event object would leave you crippled…

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

Sidebar

Related Questions

In my Java code I have function that gets file from the client in
Here's a brief rundown of my issue: I have a JavaScript function that gets
I have this function in jQuery that gets data from a page with POST,
I have a function that sending a code gets a description through various formatings,
I have a function that returns two values in a list. Both values need
I have a function that does a long task and I want to update
I have a function which gets a key from the user and generates a
I have an associative array in the form key => value where key is
I've got a Method that gets a IDictionary as a parameter. Now I want
I have a template that looks like this: <div id=PopUpTemplate style=display: none> <tr><td>${Key}</td><td>${Value}</td></tr> </div>

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.