JavaScript Answers

How to get the text of an input text box during onKeyPress with JavaScript?

Spread the love

You can get the text of an input text box during the onkeypress event in JavaScript by accessing the value property of the input element.

Here’s how you can do it:


<input type="text" id="myInput" onkeypress="getText(event)">


    function getText(event) {
        const inputText =;

In this example, we have an input text box with the id “myInput”.

We’ve added an onkeypress event handler to the input element, which calls the getText function when a key is pressed.

In the getText function, we access the value property of the input element using This gives us the current value of the input text box.

We can then use this value as needed. In this example, we’re logging it to the console.

This will log the text of the input text box to the console every time a key is pressed.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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