BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

jQuery keypress() Method

Last Updated: May 4, 2019 by Chaitanya Singh | Filed Under: jQuery

jQuery keypress() Method triggers the keypress event when any button on the keyboard is pressed down. This method doesn’t consider keys such as ALT, CTRL, SHIFT, ESC. This method attaches an html element to event handler function, this event handler function executes when the keypress event occurs.

jQuery keypress() Method Syntax

$("input").keypress(function(){
  //this code executes when the keypress event occurs.
});

Here we have selected input text field $(“input”) using jQuery selector and the keypress() method attaches this html element to the keypress event handler function, which means the keypress event will only trigger when a key is pressed inside the text box field.

jQuery keypress() Example

In the following example, we are using keypress event to count the number of characters in the input text field. To do this we have attached the input text field to the event handler function using keypress() method. This triggers the keypress event each time a key is pressed inside the text field, in the event handler function we are increasing the counter each time a key is pressed and displaying the same in the “span” area.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
<script>
count = 0;
$(document).ready(function(){
  $("input").keypress(function(){
    $("span").text(count = count+1);
  });
});
</script>
</head>
<body>

<h2>jQuery keypress event example on Beginnersbook.com</h2>
Write anything here: <input type = "text">
<p>The current characters count in the textbox is: <span>0</span></p>
</body>
</html>

Output:
Before any key is pressed:
This screenshot is taken after the page is loaded in the browser and we have performed no activity on the page.
jQuery keypress
After we have typed a word in the text field:
This screenshot is taken after we have typed a word “BeginnersBook” in the input text field. As you can see in the following screenshot, we are able to see the characters count in the span area below the input text field. The count increases simultaneously as we type the word in the text field.
jQuery keypress event

❮ PreviousNext ❯

Top Related Articles:

  1. jQuery html() method
  2. jQuery Sliding Effects
  3. jQuery change() Method
  4. jQuery keydown() Method
  5. jQuery mouseup() Method

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

jQuery Tutorial

  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events

jQuery Effects

  • jQuery Show & Hide
  • jQuery Fading
  • jQuery Sliding
  • jQuery Animate
  • jQuery stop()
  • jQuery Callback Function
  • jQuery Chaining

jQuery HTML/CSS

  • jQuery html()
  • jQuery addClass()
  • jQuery hasClass()
  • jQuery removeClass()
  • jQuery toggleClass()
  • jQuery after()
  • jQuery before()
  • jQuery append()
  • jQuery appendTo()
  • jQuery clone()
  • jQuery insertBefore()
  • jQuery insertAfter()
  • jQuery attr()
  • jQuery text()
  • jQuery val()
  • jQuery css()
  • jQuery prepend()
  • jQuery remove()
  • jQuery empty()
  • jQuery detach()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap