Menu ▼

Previous Next

Keydown event in javascript :

We will use Keydown event to execute one or more JavaScript function when a user press a key.
The "keydown" event will be fired before the "keyup" and "keypress" events.

Example:
--------------

In the below example when we pass the "event" as parameter to the function, the keydown event will passed as a parameter
and when we pass "this" as parameter, then the event calling control will be passed as parameter.

Enter value1 :
Enter value2 :
Enter value3 :
Enter value4 :

Code Sample :

<html>

<head>

    <title>keydown event example in JavaScript</title>

    <script language="Javascript">

        function keydown1(evt) {

            alert(evt.keyCode);

        }

        function keydown2(t) {

            alert(t.value);

        }

 

        function keydown3(evt, t) {

            alert(evt.keyCode);

            alert(t.value);

        }

    </script>

</head>

<body>

    <table align="center">

        <tr>

            <td>

                Enter value1 :

                <input type="text" id="Text1" onkeydown="keydown1(event);" />

            </td>

        </tr>

        <tr>

            <td>

                Enter value2 :

                <input type="text" id="Text2" onkeydown="keydown2(this);" />

            </td>

        </tr>

        <tr>

            <td>

                Enter value3 :

                <input type="text" id="Text3" onkeydown="keydown3(event,this);" />

            </td>

        </tr>

        <tr>

            <td>

                Enter value4 :

                <input type="text" id="Text4" onkeydown="keydown1(event);keydown2(this);keydown3(event,this);" />

            </td>

        </tr>

    </table>

</body>

</html>

Previous Next




Search dotnetlearners.com