I've been trying to show up the keyboard on Safari web browser when focusing on an element. I want to display the keyboard without user action in the UI.
I've followed the example provided here: IOS show keyboard on input focus
But is still not working :(
This is my HTML:
<body>
<div className="ModalBody">
This is the body of the modal. Edit this "HTML" to play with the
contents of the modal body.
<br />
<br />
<label>
The first input focuses automatically
<br />
<input type="text" />
</label>
</div>
</body>
This is my JS:
function focusAndOpenKeyboard(el, timeout) {
if(!timeout) {
timeout = 100;
}
if(el) {
// Align temp input element approximately where the input element is
// so the cursor doesn't jump around
var __tempEl__ = document.createElement('input');
__tempEl__.style.position = 'absolute';
__tempEl__.style.top = (el.offsetTop + 7) + 'px';
__tempEl__.style.left = el.offsetLeft + 'px';
__tempEl__.style.height = 0;
__tempEl__.style.opacity = 0;
// Put this temp element as a child of the page <body> and focus on it
document.body.appendChild(__tempEl__);
__tempEl__.focus();
__tempEl__.click();
// The keyboard is open. Now do a delayed focus on the target element
setTimeout(function() {
el.focus();
// Remove the temp element
document.body.removeChild(__tempEl__);
}, timeout);
}
}
var firstInputEl = document.querySelector("input, textarea");
if (firstInputEl) {
focusAndOpenKeyboard(firstInputEl, 300);
}
The element ends up focused but the keyboard is not showing up on my iPhone.
Any ideas? Thanks!