0

I am trying to focus an input element after an alert message in Google Chrome on Android.

The problem is that the focus isnt set on the input element. The keyboard opens but I cant type anything in.

This is my input:

<input id="input" type="text">
<button id="button">Button</button>

I am trying to focus the input on button click

const btn = document.getElementById('button')
btn.addEventListener('click', () => {
    alert("Some information")
    const input = document.getElementById('input')
    input.focus()
})

I tried to add a timeOut to focus but it didnt work.

Is this a Chrome Mobile problem? Because in Firefox Mobile its working as expected.

  • At the beginning of your quesiton, you mentioned having `alert(...)`, but there's no `alert` in the code you posted. Can you post your exact code, trimmed down to the relevant part? – FiddlingAway Jan 03 '23 at 12:36
  • @FiddlingAway Sorry, I forgot the alert - I edited the question Without the alert it also focuses in Chrome, but with alert its not working – Dr Revolution Jan 03 '23 at 12:42
  • So the `alert` is causing the issue. You need to replace it with something else - display that information in a `div` with `position: fixed`, for example. Or in a modal of your own making. Then, once the user dismisses the notification / modal, focus back to the input. – FiddlingAway Jan 03 '23 at 13:01
  • Just tested the code on my smartphone (iOS), it works just fine on Safari and Chrome, but you mentioned using “Android”; Have you tested it on another Android Device? – maiakd Jan 03 '23 at 13:04
  • @maiakd Tested it on my smartphone (S22 Ultra) and another android device - in both cases it didnt work – Dr Revolution Jan 03 '23 at 13:09
  • Can you try `confirm()` instead of `alert()`: `if (confirm('Some information')){input.focus() }`? I don't have an android nearby today to do the testings myself – maiakd Jan 03 '23 at 13:37

0 Answers0