I am developing an HTML code editor using simple DIV\'s and capturing events. When I use this on the iPad the keyboard never pops up since i\'m not technically in an editab
To make the keyboard show on iOS devices you need to focus on an editable element such as an input
or a textarea
. Furthermore, the element must be visible and the .focus()
function must to be executed in response to a user interaction such as a mouse click.
The thing is - we DON'T want the input element to be visible.. I have fiddled with this for quiet some time and eventually got the result I was looking for.
First, create an element you want to use to show the keyboard - in this case a button, and a hidden input element: (Working jsFiddle or Test on a mobile device)
<button id="openKeyboard">Open Keyboard</button>
<input id="hiddenInput" style="visibility: hidden;">
Then use the following javascript:
document.getElementById('openKeyboard').addEventListener('click', function(){
var inputElement = document.getElementById('hiddenInput');
inputElement.style.visibility = 'visible'; // unhide the input
inputElement.focus(); // focus on it so keyboard pops
inputElement.style.visibility = 'hidden'; // hide it again
});
Notes:
opacity
, height
and width
to 0
. However, if your input is completely hidden this won't work again, so make sure you leave the padding
or border
just so there's something to be rendered (even though it won't show up due to the opacity). This also means you shouldn't use display:none
to hide it, hidden elements are just not allowed to be focused.keydown
, keypress
, keyup
) on your hidden input to access the user's interaction as you would normally do. Nothing special here.