• 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
  • VBA
  • About
You are here: Home / JavaScript / JavaScript onkeyup – How to Use onkeyup Event with JavaScript

JavaScript onkeyup – How to Use onkeyup Event with JavaScript

February 13, 2022 Leave a Comment

We can use the onkeyup 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 onkeyup event in the HTML.

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

The onkeyup event is very similar to the onkeydown 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 keyup() method.

A JavaScript example using the onkeyup 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" onkeyup="keyPressed(event)">
  <div id="result"></div>
</div>

We will use the onkeyup 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 onkeyup 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" onkeyup="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 onkeyup event.

Other Articles You'll Also Like:

  • 1.  Math abs JavaScript – How to get the absolute value of a number using JavaScript
  • 2.  JavaScript atan – Find Arctangent and Inverse Tangent of Number
  • 3.  Using JavaScript to Show a Div
  • 4.  Insert Character Into String In JavaScript
  • 5.  Clicking on an Image to Enlarge it in HTML
  • 6.  Using JavaScript to Check if Variable is a Number
  • 7.  Using JavaScript to Get the URL Path
  • 8.  JavaScript slice – How to Get a Substring From a String
  • 9.  JavaScript Change Background Color of Element
  • 10.  JavaScript Square Root with Math.sqrt() Method

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy