JavaScript What objects can trigger the key press event in the DOM?

I've been experimenting and it seems like only form elements and the window object can trigger the keypress event.

I checked MDN documentation, and also the spec, but couldn't find anywhere that listed valid objects that can trigger this event. In the code sample below, if I comment out the line beginning "window" then I get no console message, implying that the div element is not triggering the key press event, however, if I leave in the line to define a handler for the key press event on the window, then I do see the console messages.

Can anyone point me to something (or confirm/list) the valid objects that can trigger the keypress event? Thanks!

window.onload = function() {
    var div = document.getElementById("div");
    div.onclick = clickMeHandler;
    div.onkeypress = clickMeHandler;
    window.onkeypress = clickMeHandler;
};
function clickMeHandler(e) {
    console.log("click me handler, type: ", e.type);
    console.log("click me handler, target: ", e.target);
}
Answer:1

Haven't gotten the full list of elements keypress applies to, but I believe you're correct - only controls were keypress makes sense - have it. Taking your example - a DIV can have keypress event but only when you can actually type something into it, namely when it has attribute set contenteditable="true"

Demo: http://jsfiddle.net/qny4A/

UPDATE

Actually I stand corrected. I beleive pretty much any element can generate keypress event as long as it can receive focus, which can be achived via "tabindex" attribute

Here is a demo of a SPAN that generates the event when it's in focus: http://jsfiddle.net/qny4A/1/

Answer:2

When defining a class in Javascript, how can I call one method from another one? exports.myClass = function () { this.init = function() { myInternalMethod(); } this....

When defining a class in Javascript, how can I call one method from another one? exports.myClass = function () { this.init = function() { myInternalMethod(); } this....

  1. calling method from another class
  2. calling method from another class c#
  3. calling method from another class python
  4. calling method from different class java
  5. calling method from main java
  6. calling method from parent class python
  7. calling method from same class java
  8. calling method from superclass java
  9. calling method from class python
  10. calling method from constructor java
  11. calling method from abstract class java
  12. calling method from parent class java
  13. calling method from another component angular
  14. calling method from this
  15. call method from another activity android
  16. call method from another class react
  17. call method from another script unity
  18. call method from activity to fragment
  19. call method from another controller laravel
  20. call method from another component vue

I'm wanting to use jQuery to dynamically modify the background color of a <div>, based on its CSS property, width value. The usage is for some form of color-coded meter, which indicates how ...

I'm wanting to use jQuery to dynamically modify the background color of a <div>, based on its CSS property, width value. The usage is for some form of color-coded meter, which indicates how ...

  1. change background color in photoshop
  2. change background color google docs
  3. change background color html
  4. change background color instagram story
  5. change background color illustrator
  6. change background color in word
  7. change background color css
  8. change background color indesign
  9. change background color on iphone
  10. change background color javascript
  11. change background color in outlook
  12. change background color using css mdn
  13. change background color online
  14. change background color after effects
  15. change background color of picture
  16. change background color excel
  17. change background color gimp
  18. change background color premiere
  19. change background color instagram story video
  20. change background color in pages

I need to get the window inner height of the window, taking into account the x-scroll bar. but it seems window.innerHeight returns the same height regardless of whether or not the x-scroll bar is ...

I need to get the window inner height of the window, taking into account the x-scroll bar. but it seems window.innerHeight returns the same height regardless of whether or not the x-scroll bar is ...

  1. window innerheight vs outerheight
  2. window innerheight css
  3. window innerheight mobile
  4. window innerheight iframe
  5. window innerheight jquery
  6. window innerheight javascript
  7. window.innerheight
  8. window.innerheight vs document.documentelement.clientheight
  9. window.innerheight vs screen.height
  10. window.innerheight not working
  11. window.innerheight vs document.body.clientheight
  12. window.innerheight js
  13. window.innerheight react
  14. window.innerheight safari mobile
  15. window.innerheight undefined
  16. window.innerheight document.documentelement.clientheight
  17. window.innerheight scrollbar
  18. window.innerheight browser support
  19. window.innerheight vs clientheight
  20. window.innerheight too big

I'm working on a REST interface to a library system that uses the SIP2 protocol (https://en.wikipedia.org/wiki/Standard_Interchange_Protocol) and was able to get things working on a system that doesn'...

I'm working on a REST interface to a library system that uses the SIP2 protocol (https://en.wikipedia.org/wiki/Standard_Interchange_Protocol) and was able to get things working on a system that doesn'...