Categories
JavaScript Answers

How to capture tab key in text box with JavaScript?

Spread the love

Sometimes, we want to capture tab key in text box with JavaScript.

In this article, we’ll look at how to capture tab key in text box with JavaScript.

How to capture tab key in text box with JavaScript?

To capture tab key in text box with JavaScript, we can use the event keyCode property.

For instance, we write

document.onkeydown = (evt) => {
  const tabKey = 9;
  if (evt.keyCode === tabKey) {
    // ...
  }
};

to listen to the keydown event on the document by setting the document.onkeydown property to a function when we press a key anywhere on the page.

In the function, we check if evt.keyCode is 9.

If it is, then the tab key is pressed on the page.

Conclusion

To capture tab key in text box with JavaScript, we can use the event keyCode property.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.