Skip to main content

Exercise 37

  • Create the following folder/file structure:
/ex_37
|-- index.html
<!DOCTYPE html>
<html>
<head>
<title>Events</title>
</head>
<body>
<div></div>
<button id="firstButton">Click Me!!</button>
<button id="secondButton">Remove event listener</button>
<a href="https://google.com">Link with JavaScript handler</a>
</body>
</html>

index.html

  • Create a basic HTML document
  • Create a script tag on the document head element
  • Select the body element and add the keypress event handler
  • When the user press a key we'll show the following message on the console
You pressed the %pressedKey% key with the %charCode% code
  • Use event.charCode & String.fromCharCode() to get the key and code