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?
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.
const hideMobileKeyboardOnReturn = (keyboardEvent) => { element. addEventListener('keyup', (keyboardEvent) => { if (keyboardEvent. code === 'Enter') { element. blur(); } }); }; document.
I found a simpler solution that requires neither adding element nor a special class. found it there: http://www.sencha.com/forum/archive/index.php/t-141560.html
And converted the code to jquery :
function hideKeyboard(element) { element.attr('readonly', 'readonly'); // Force keyboard to hide on input field. element.attr('disabled', 'true'); // Force keyboard to hide on textarea field. setTimeout(function() { element.blur(); //actually close the keyboard // Remove readonly attribute after keyboard is hidden. element.removeAttr('readonly'); element.removeAttr('disabled'); }, 100); }
You call the function by passing it the input from which the keyboard was opened, or just passing $('input') should also work.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With