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

Spread the love

To get the text of an input text box during onKeyPress with JavaScript, we use thw value property.

For instance, we write

const onKeyPress = () => {
  const inputelem = document.getElementById("editvalue");
  const s = inputelem.value;
};

const inputElem = document.getElementById("editvalue");
inputElem.addEventListener("keypress", onKeyPress);

to get the input with getElementById.

And then we call addEventListener to add a keypress onKeyPress as the event listener.

In onKeyPress, we get the input with getElementById.

And we get its value with the value property.

Leave a Reply

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