• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript onkeydown – How to Use onkeydown Event with JavaScript

JavaScript onkeydown – How to Use onkeydown Event with JavaScript

February 13, 2022 Leave a Comment

We can use the onkeydown event to run a function when a user types something into an input field. To do this we can add a function call to the onkeydown event in the HTML.

<input type="text" onkeydown="funct1()">

The onkeydown event is very similar to the onkeyup event, the main difference being that the onkeydown event will fire once a key is pressed, while the onkeyup event will only run once the key is pressed AND released.

This can also be done in jQuery using the keydown() method.

A JavaScript example using the onkeydown event

In this example, we will just have a simple input field with a div below it. When a user enters a key in the input field, we will display what key was pressed below. Here is the HTML setup:

<div id="div1">
  <p>Type any key below</p>
  <input type="text" id="textInput" onkeydown="keyPressed(event)">
  <div id="result"></div>
</div>

We will use the onkeydown event to run the function below. The function will retrieve the key that was pressed and display that below in the results div using the textContent property.

Here is the JavaScript code:

function keyPressed(e){
  var key = e.key;
  document.getElementById("result").textContent = key;
};

The final code and output for this example of using the onkeydown event is below:

Code Output:

Type any key below

Full Code:

<div id="div1">
  <p>Type any key below</p>
  <input type="text" id="textInput" onkeydown="keyPressed(event)">
  <div id="result"></div>
</div>

<script>

function keyPressed(e){
  var key = e.key;
  document.getElementById("result").textContent = key;
};

</script>

Hopefully this article has been useful to help you understand how to use the onkeydown event.

Other Articles You'll Also Like:

  • 1.  Using JavaScript Math Module to Get Euler’s Constant e
  • 2.  Sum the Digits of a Number in JavaScript
  • 3.  Using JavaScript to Check if Variable is a Function
  • 4.  Set Hidden Field Value In JavaScript
  • 5.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 6.  Using JavaScript to Get the Height of an Element
  • 7.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 8.  Using JavaScript to Format the Date in mm dd yyyy
  • 9.  Using JavaScript to Check if Number is Divisible by Another Number
  • 10.  How to Clear a Textarea Field In JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy