Created
January 26, 2014 12:20
-
-
Save WebPlatformDocs/8631904 to your computer and use it in GitHub Desktop.
keypress event demonstration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* keypress event demonstration */ | |
form { | |
padding-top: 30px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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