Skip to content
Advertisement

How can I hide the Android keyboard using JavaScript?

I would like to hide the Android virtual keyboard in JavaScript. Someone suggested doing this:

$('#input').focus(function() {
  this.blur();
});

But this doesn’t work if the keyboard is already visible. Is this something that can be done?

Advertisement

Answer

What you need to do is create a new input field, append it to the body, focus it and the hide it using display:none. You will need to enclose these inside some setTimeouts unfortunately to make this work.

var field = document.createElement('input');
field.setAttribute('type', 'text');
document.body.appendChild(field);

setTimeout(function() {
    field.focus();
    setTimeout(function() {
        field.setAttribute('style', 'display:none;');
    }, 50);
}, 50);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement