• 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 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.  Creating a JavaScript Function to Divide Two Numbers
  • 2.  JavaScript Trig – How to Use Trigonometric Functions in Javascript
  • 3.  Using Javascript to Remove the Id from a Div
  • 4.  JavaScript acosh – Find Hyperbolic Arccosine of Number
  • 5.  Using JavaScript to Get the Page Title
  • 6.  Grouping By Multiple Properties and Summing Using Javascript
  • 7.  How to Select All Checkboxes in JavaScript
  • 8.  Using JavaScript to Convert String to Integer
  • 9.  JavaScript Array includes Method
  • 10.  Remove Parentheses From String Using 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