Reputation: 864
I want hide virtual keyboard on ipad device when some plugin ( or other code) set focus to input element on html page using pure javascript( or jquery library)
Upvotes: 4
Views: 21649
Reputation: 3631
This is an old question that I recently found a better solution for, works with iOS 13+. If you do not want to blur your input, you can control virtual keyboard with inputMode
. Setting it to none
hides keyboard. Setting it to something else shows the corresponding keyboard. Check out this demo:
https://codepen.io/waterplea/pen/dyLjaQP
Upvotes: 0
Reputation: 839
Sometimes, it needs waiting for a while.
setTimeout(function() { document.activeElement.blur(); }, 100);
Upvotes: 1
Reputation: 316
Typescript version requires to fire blur()
on HTMLElement
. Like this:
let myElement: HTMLElement;
myElement = <HTMLElement>document.activeElement;
myElement.blur()
Upvotes: 1
Reputation: 12854
If you need a pure javascript solution, use this line :
document.activeElement.blur();
This line removes the focus on the active element and hiding the keyboard.
Reference
Upvotes: 19
Reputation: 6655
You would have to blur it again but the keyboard might flash.
$('input').on('focus', function() { $(this).blur(); });
Or, depending on if you have dynamically created input
elements.
$('#wrapper').on('focus', 'input', function() { $(this).blur(); });
Upvotes: 4