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 keydown() Method

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

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

keypress() vs keydown()

The keypress() Method doesn’t trigger keypress event when any of the special keys such as ALT, CTRL, SHIFT, ESC are pressed, if you want to check for these keys then use keydown() method instead.

jQuery keydown() Method Syntax

$("input").keydown(function(){
  //this code executes when a key is pressed down. 
});

Here we have attached the event handler function to input text field ($(“input”)) using keydown() method, this way the keydown event will only trigger when the key is pressed inside the text field. If you like, you can select any other html element using jQuery selector.

jQuery keydown() Example

In the following example we have attached the input text field to event handler function using keydown() method. This makes the keydown event to trigger when a key is pressed down inside the input field.

Here we are changing the background colour of the input field inside the event handler function, which means when the keydown event triggers, the background colour of input field changes.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("input").keydown(function(){
    $("input").css("background-color", "green");
  });
});
</script>
</head>
<body>

<h2>jQuery keydown event example on Beginnersbook.com</h2>
Write anything here: <input type = "text">
<p>The background color of input text field will change to green, when you
press a key inside the input field.</p>
</body>
</html>

Output:
Before a key is pressed down:
This screenshot is taken before we have performed any activity on the page.
jQuery keydown
After a key is pressed down inside the input text field:
This screenshot is taken after we have pressed down a key inside input field. As you can see in the screenshot that the background colour of the input field changes to green.
jQuery keydown event

❮ PreviousNext ❯

Top Related Articles:

  1. jQuery html() method
  2. jQuery Sliding Effects
  3. jQuery keypress() Method
  4. jQuery change() Method
  5. jQuery val()

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