Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 590
Yoh Suzuki Avatar asked Dec 01 '11 02:12

Yoh Suzuki


People also ask

How do I make my keyboard invisible on Android?

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.

How can I hide my mobile keyboard?

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.

How do I close a Javascript keyboard?

const hideMobileKeyboardOnReturn = (keyboardEvent) => { element. addEventListener('keyup', (keyboardEvent) => { if (keyboardEvent. code === 'Enter') { element. blur(); } }); }; document.


2 Answers

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.

like image 104
QuickFix Avatar answered Sep 19 '22 18:09

QuickFix


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); 
like image 21
rdougan Avatar answered Sep 17 '22 18:09

rdougan