Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onkeypress does not call function

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.

like image 462
Youssef Moawad Avatar asked Jun 01 '26 12:06

Youssef Moawad


2 Answers

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>
like image 164
joyBlanks Avatar answered Jun 03 '26 01:06

joyBlanks


checkValidity is Javascript's form validation method. Change the method name. Then it will work.

like image 31
mable george Avatar answered Jun 03 '26 00:06

mable george



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!