Show virtual keyboard on mobile phones in javascript

Adam picture Adam · Jul 27, 2011 · Viewed 66.8k times · Source

I'm creating a mobile version of my site. There's a part of the site where a dialog pops up with a text input. Normally I would just use jQuery to bring focus to the text input, but that's not working. Here's what I'm trying:

$("#textinput").focus();
$("#textinput").click(); 
$("#textinput").trigger("tap"); //jQuery Mobile

None of them seem to work. Any ideas? I've been testing it on my DroidX. I'm using jQuery Mobile but I'm open to other libraries if they help.

Answer

Mark Kahn picture Mark Kahn · Jul 27, 2011

You can't, at least not in iOS (iPhone), and I believe Android as well. It's a usability issue that the keyboard should not be allowed to be triggered except by user input (it's just annoying if it's automatic).

There are a couple of ways I know of to get around this:

  • prompt() opens the keyboard
  • If you trigger the .focus() from within a .click() event (e.g. from opening your dialog), the keyboard shows up