In JavaScript, pressing a key triggers events which can be captured and handled. Three events are triggered when a key is pressed and released:
Many of us confusing regarding these key events,here we have the clarification
The KeyDown event is triggered when the user presses a Key.
The KeyUp event is triggered when the user releases a Key.
The KeyPress event is triggered when the user presses & releases a Key. (onKeyDown followed by onKeyUp)
The following code will elaborate you exactly how these threee events are different
<!doctype html>
<html lang="en">
<head>
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script>
var i=0;
function change1(){
document.getElementById('text2').value= "onkeyDown";
}
function change2(){
document.getElementById('text2').value= "onkeyUp";
}
function change3(){
i++;
document.getElementById('text3').value= "keyPressed "+i+" stimes";
}
</script>
</head>
<body bgcolor="white">
<input type="text" onkeyDown="change1()" onkeyUp="change2()"onkeyPress="change3()" id="text1" >
</body>
<input type="text" id='text2'/>
<input type="text" id='text3'/>
</html>
Output:
No comments:
Post a Comment