Definition and Usage..
The onkeypress event occurs when the user presses a key (on the keyboard).
Syntax:
In HTML: <element onkeypress="myScript">
In JavaScript: object.onkeypress = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("keypress", myScript)
CODING:The onkeypress event occurs when the user presses a key (on the keyboard).
Syntax:
In HTML: <element onkeypress="myScript">
In JavaScript: object.onkeypress = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("keypress", myScript)
<!DOCTYPE html>
<html>
<body>
<p>In HTML:</p>
<input type="text" id="text0" onkeypress="Fun()">
<script>
function Fun() {
document.getElementById("text0").value="kartik";
}
</script>
<p>In JavaScript:</p>
<input type="text" id="text1">
<script>
document.getElementById("text1").onkeypress = 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("keypress", fun2);
function fun2() {
document.getElementById("text2").style.width = "180px";
}
</script>
</body>
</html>
<html>
<body>
<p>In HTML:</p>
<input type="text" id="text0" onkeypress="Fun()">
<script>
function Fun() {
document.getElementById("text0").value="kartik";
}
</script>
<p>In JavaScript:</p>
<input type="text" id="text1">
<script>
document.getElementById("text1").onkeypress = 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("keypress", fun2);
function fun2() {
document.getElementById("text2").style.width = "180px";
}
</script>
</body>
</html>
Comments
Post a Comment