Search code examples
javascripthtmliosvue.jssafari

IOS show keyboard on input focus


I have a problem that i can't fix.

Keyboard doesn't show on input.focus() on IOS

 searchMobileToggle.addEventListener('click', function() {
       setTimeout(function(){
          searchField.focus();
       }, 300);
    });

I've been looking for a solution with no result, i know this is a frequently unsolved question but i see NIKE (https://m.nike.com/fr/fr_fr/) and FOODSPRING (https://www.foodspring.fr/) doing it on mobile.

So i'm wondering how do they do ?


Solution

  • I found a solution, click() didn't work, but i figured it out.

    searchMobileToggle.addEventListener('click', function() {
             if(mobileSearchblock.classList.contains('active')) {
                searchField.setAttribute('autofocus', 'autofocus');
                searchField.focus();
            }
            else {
                searchField.removeAttribute('autofocus');
            }
        });
    

    I was working with vue.js that was removing input autofocus attribute, when the component was loaded. So i had it on click, but there was another problem, the autofocus only worked once, but combined with focus(), it now work all the time :)

    Thanks for your help !