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 8837735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:48:09+00:00 2026-06-14T09:48:09+00:00

When dealing with keyboard event, I realise that not all element can get the

  • 0

When dealing with keyboard event, I realise that not all element can get the event target. For example, when adding a handler to a element that does not has keyboard focus (the “p” element) like the followings, then, ofcourse nothing will happens when I press a keyboard.

However, if I change “p” element by the whole document to add that keyboard event (let’s say by changing my code to document.keydown = myFunction; ), then the code functions well. And as I read somewhere, this is because the keyboard event happened somewhere in document and then buble up to the document element. So could you tell me where this event happens in document?
Here is the code:

<!DOCTYPE html>
<html>
<head>
</head>
<body>    
<p id = 'myId'>Some Text</p>

<script>

function myFunction(e)
{
var x=e.keyCode;
var keychar=String.fromCharCode(x);
alert("Key " + keychar + " was pressed down");
}
var myEl = document.getElementById('myId');
myEl.onkeydown = myFunction;

</script>
</body>
</html>

Thank you!

  • 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-14T09:48:10+00:00Added an answer on June 14, 2026 at 9:48 am

    The fact that adding an onkeydown event handler to the document element works shouldn’t necessarily be taken as an indication that the event is bubbling up to the document element.

    Essentially what that instruction is telling the browser is “while the user is viewing this page, respond to any keydown event with this function”.

    If you had a text input within the document and typed into that input, your handler would still react in the same way, and then one could say that the event was bubbling up from that element through any containing elements all the way up to level of the document. But without such an element – i.e. one intended to react to key input events – your keydown event is occurring at the level of the document, not below.

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

Sidebar

Related Questions

I am trying to simulate keyboard event (for F1 - F12) and dispatch that
Dealing with go's funcs I discovered that one can't force the compiler to control
Dealing with classes (nested etc) does not look easy in Python , surprisingly !
Dealing with some legacy code and in trying to get a poorly designed database
I'm dealing with a problem the whole week already and can't find the answer
I am dealing with very primitive HTML construction that goes like this: <a NAME=header1></a><b><font
when dealing with wildcards such as setting/adding a generic item to a certain container
When dealing with a flash application that will run in a browser, it's obvious
When dealing with websites with large amount of javascript, i see that these are
When dealing with something like a List<string> you can write the following: list.ForEach(x =>

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.