JavaScript keycode space bar | Example code

JavaScript keycode of space bar is 32. These keycode values are only valid during in keydown and keyup events. On Mac, keypress events give you an completely different set of codes.

Must Read: JavaScript keycode list

Example spacebar key code JavaScript

HTML example code execute JS code after pressing space bar.

<!DOCTYPE html>
<html>
<body>

  <script>
    document.body.onkeyup = function(e){
      if(e.keyCode == 32){
        alert(e.code);
      }
    }

  </script>

</body>
</html>

Output:

JavaScript keycode space bar

Do comment if you have any doubts and suggestions on this JS Keycode tutorial.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.