I need to call a js function everytime a key is pressed in a textbox:
<html>
<script>
function checkValidity() {
alert("Checking...");
}
</script>
<body>
<input type="text" name="username" id="username" onkeypress="checkValidity()">
</body>
</html>
The alert, however, never get displayed. What's wrong with this code?
UPDATE
The script is actually in a separate js file but I'm certain it's declared correctly because if I call checkValidity() from the body's onload I do get the alert.
Actually your markup is right but the function name checkValidity is now an internal keyword for elements
Check here https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/checkValidity
You can change the name of the function thats all
<html>
<script>
function checkMyValidity() {
alert("Checking...");
}
</script>
<body>
<input type="text" name="username" id="username" onkeypress="checkMyValidity()">
</body>
</html>
checkValidity is Javascript's form validation method. Change the method name. Then it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With