Yoh Suzuki Yoh Suzuki - 2 months ago 19
Android Question

How can I hide the Android keyboard using JavaScript?

I would like to hide the Android virtual keyboard in JavaScript. Someone suggested doing this (http://stackoverflow.com/questions/7045889/how-to-hide-keyboard-on-phonegap-android):

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


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

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);
Comments