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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  Using JavaScript to Remove Forward Slash From String
  • 2.  Using JavaScript to Subtract Days From a Date
  • 3.  JavaScript isPrime – How to Check if a Number is Prime in JavaScript
  • 4.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 5.  Reverse a String in JavaScript
  • 6.  How to Change the Id of an Element in JavaScript
  • 7.  Using JavaScript to Capitalize the First Letter of a String
  • 8.  Convert String to Float in JavaScript
  • 9.  Using JavaScript to Get the Decimal Part of a Number
  • 10.  Using JavaScript to Check if Variable Exists

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