Categories
JavaScript Answers

How to track onchange when typing in text input with JavaScript?

Spread the love

Sometimes, we want to track onchange when typing in text input with JavaScript.

In this article, we’ll look at how to track onchange when typing in text input with JavaScript.

How to track onchange when typing in text input with JavaScript?

To track onchange when typing in text input with JavaScript, we listen to the input event.

For instance, we write

const source = document.getElementById("source");

const inputHandler = (e) => {
  console.log(e.target.value);
};

source.addEventListener("input", inputHandler);

to select the input with getElementById.

Then we call addEventListener to listen for the input event in the input.

inputHandler is called when we change the input’s value.

Conclusion

To track onchange when typing in text input with JavaScript, we listen to the input event.

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 *