Definition and Usage..
The onkeydown event occurs when the user is pressing a key (on the keyboard).
Syntax:
In HTML: <element onkeydown="myScript">
In JavaScript: object.onkeydown = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("keydown", myScript);
CODING:The onkeydown event occurs when the user is pressing a key (on the keyboard).
Syntax:
In HTML: <element onkeydown="myScript">
In JavaScript: object.onkeydown = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("keydown", myScript);
<!DOCTYPE html>
<html>
<body>
<p>In HTML:</p>
<input type="text" id="text0" onkeydown="Fun()">
<script>
function Fun() {
document.getElementById("text0").value="kartik";
}
</script>
<p>In JavaScript:</p>
<input type="text" id="text1">
<script>
document.getElementById("text1").onkeydown = function() {fun1()};
function fun1() {
document.getElementById("text1").style.backgroundColor = "red";
}
</script>
<p>In JavaScript, using the addEventListener() method:</p>
<input type="text" id="text2" style="width:20px;">
<script>
document.getElementById("text2").addEventListener("keydown", fun2);
function fun2() {
document.getElementById("text2").style.width = "180px";
}
</script>
</body>
</html>
<html>
<body>
<p>In HTML:</p>
<input type="text" id="text0" onkeydown="Fun()">
<script>
function Fun() {
document.getElementById("text0").value="kartik";
}
</script>
<p>In JavaScript:</p>
<input type="text" id="text1">
<script>
document.getElementById("text1").onkeydown = function() {fun1()};
function fun1() {
document.getElementById("text1").style.backgroundColor = "red";
}
</script>
<p>In JavaScript, using the addEventListener() method:</p>
<input type="text" id="text2" style="width:20px;">
<script>
document.getElementById("text2").addEventListener("keydown", fun2);
function fun2() {
document.getElementById("text2").style.width = "180px";
}
</script>
</body>
</html>
Comments
Post a Comment