EVENT:  onkeydown

onkeydown="action"
 
The onkeydown event is associated with pressing down a key on the keyboard while the mouse is inside a selected HTML tag (element). In other words, the key down event occurred while the HTML element was in focus. Whenever you press down the key, the script code of the onkeydown is executed. This script can also call functions or subroutines which contain code that you want to run when this event occurs. For example, if you only want numbers to be entered, you could capture the value of the key and take appropriate action if a non-numeric key was pressed.
 
The similar onkeyup event is associated with the release of a key after it was pressed down, and the onkeypress event is associated with the pressing of a key.
 
In this example, place the mouse inside the textarea element and type in a word. Every time you press a key down, one line of JavaScript code is executed which causes an alert box to be displayed with a message. Note how the alert comes up before the character is displayed inside the textarea.
 
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="eng">
<head>
<title>DevGuru XHTML onkeydown Tag Example</title>
</head>
<body>
<textarea onkeydown="javascript: alert('A key was pressed down')" >
</textarea>
</body>
</html>

 
Output:


Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information