Get key event target element id in JavaScript
Description
The following code shows how to get key event target element id.
Example
<html>
<head>
<script type="text/javascript">
function handleEvent(oEvent) {<!-- www . j ava 2s. c om-->
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n>" + oEvent.type;
oTextbox.value += "\n target is " + (oEvent.target || oEvent.srcElement).id;
}
</script>
</head>
<body>
<P>Type some characters into the first textbox.</p>
<P><textarea id="txtInput" rows="15" cols="50"
onkeypress="handleEvent(event)"></textarea></p>
<P><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Key Event
Check if Alt key is pressed during the even...
Check if Shift key is pressed during the ev...
Check if the Control key is pressed during ...
Convert the key code from key event to char...
Get the key code from key event in JavaScri...
Get the key event target or event source in...
Listen to body on key down event in JavaScr...
Listen to key down event for TextArea in Ja...
Listen to key down event for input text ele...
Listen to key press event for TextArea in J...
Listen to key up event for TextArea in Java...
Check if Alt key is pressed during the even...
Check if Shift key is pressed during the ev...
Check if the Control key is pressed during ...
Convert the key code from key event to char...
Get key event target element id in JavaScri...
Get the key char from the key event in Java...Get the key code from key event in JavaScri...
Get the key event target or event source in...
Listen to body on key down event in JavaScr...
Listen to key down event for TextArea in Ja...
Listen to key down event for input text ele...
Listen to key press event for TextArea in J...
Listen to key up event for TextArea in Java...