Skip to content

Instantly share code, notes, and snippets.

@WebPlatformDocs
Created January 26, 2014 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebPlatformDocs/8631904 to your computer and use it in GitHub Desktop.
Save WebPlatformDocs/8631904 to your computer and use it in GitHub Desktop.
keypress event demonstration
/* keypress event demonstration */
form {
padding-top: 30px;
}
<form>
<label for="keypress">Keypress</label>
<input name="keypress"/>
</form>
<div id="target">
<p>The last key code you entered for keypress is: <span id="keypressEcho"></span></p>
</div>
// Getting the target elements from the DOM that we would like to mess with.
var keypressInput = document.getElementsByName('keypress')[0];
var keypressTarget = document.getElementById('keypressEcho');
// This is going to run the function whenever a keypress event occurs on the Input element.
keypressInput.addEventListener('keypress', function(e) {
//Change the text of the target element to be the number of the key pressed. (number is based on the ASCII key standard.)
keypressTarget.textContent = e.which;
// Log the key event as well.
console.log(e.which);
});
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"all"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment