How to use JQuery Event keypress() Method

This article describe about jQuery Event keypress() Method.
  • 2883

jQuery Event keypress() Method

The keypress event occurs when a button is pressed down. It is similar to the keydown event. Occurs on the element that currently has focus.

keypress event occur for each inserted character, unlike the key down event.

Example

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    i = 0;

    $(document).ready(function () {

        $("input").keypress(function () {

            $("span").text(i += 1);

        });

    });

</script>

</head>

<body>

Enter name: <input type="text" />

<p>Keypresses:<span>0</span></p>

</body>

</html>

 

Output

 

keypress() method pic39.jpg

 

Trigger the keypress Event

Trigger generate the keypress for selected element.

 

Example

 

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    i = 0;

    $(document).ready(function () {

        $("input").keypress(function () {

            $("span").text(i += 1);

        });

        $("button").click(function () {

            $("input").keypress();

        });

    });

</script>

</head>

<body>

Enter name: <input type="text" />

<p>Keypresses:<span>0</span></p>

<button>Activate keypress event for input field.</button>

</body>

</html>

 

Output


keypress() method pic40.jpg

 

Note: Keypress event increment after on the button click.

 

Bind a Function to the keypress Event

It is define a function to run when keypress event is triggered for selected element

Syntax

$(selector).keypress(function)

 You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.