Anna Kallivayalil
Anna Kallivayalil

Reputation: 122

How to take input on a mobile site without a input field

The user needs to press the key X for the function to run and it works perfectly where there is a physical keyboard but how can I force a virtual keyboard on the mobile version? I can't think of a way to do this without adding an input field.

document.addEventListener("keyup", function(event) {
  if (event.keyCode === 88) {
   newx();
  }
});
function newx()
{alert("Hello");
}
<h1>Key in X</h1>

The repl I'm using this for -> https://home.ajkallivayalil.repl.co/

Upvotes: 1

Views: 612

Answers (1)

Anvay
Anvay

Reputation: 363

After some searching on Google, I came up with this, but I suggest you make another user experience, as @95faf8e76605e973 said. (e.g. a button)

If you have some sort of input field and make it hidden, like using display: none or visibility: hidden, you can use JavaScript to activate it anytime, thus bringing up a keyboard.

To do this, you just have to use .focus() on your element.

A link to the SO page I found.

Upvotes: 2

Related Questions