alx lark
alx lark

Reputation: 864

hide keyboard on ios device when focus in input

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

Answers (5)

waterplea
waterplea

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

egemen
egemen

Reputation: 839

Sometimes, it needs waiting for a while.

setTimeout(function() { document.activeElement.blur(); }, 100);

Upvotes: 1

palucdev
palucdev

Reputation: 316

Typescript version requires to fire blur() on HTMLElement. Like this:

let myElement: HTMLElement;
myElement = <HTMLElement>document.activeElement;
myElement.blur()

Upvotes: 1

R3tep
R3tep

Reputation: 12854

If you need a pure solution, use this line :

document.activeElement.blur();

This line removes the focus on the active element and hiding the keyboard.


Reference

Upvotes: 19

ryan
ryan

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

Related Questions