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>

Click to view the demo

The code above generates the following result.

Get key event target element id in JavaScript