Categories
JavaScript Answers

How to hide the Android keyboard using JavaScript?

Spread the love

Sometimes, we want to hide the Android keyboard using JavaScript.

In this article, we’ll look at how to hide the Android keyboard using JavaScript.

How to hide the Android keyboard using JavaScript?

To hide the Android keyboard using JavaScript, we set hide it after we focus on the input.

For instance, we write

const field = document.createElement("input");
field.setAttribute("type", "text");
document.body.appendChild(field);

setTimeout(() => {
  field.focus();
  setTimeout(() => {
    field.setAttribute("style", "display: none;");
  }, 50);
}, 50);

to call field.focus to focus on the input.

Then we call field.setAttribute with 'style' and 'display: none' to hide the field.

We do both with a delay by putting them in a setTimeout callback.

Conclusion

To hide the Android keyboard using JavaScript, we set hide it after we focus on the input.

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 *