document.getElementById(id).focus() is not working for firefox or chrome

javascript

Solution

Try using a timer:

const id = "mobileno";
const element = document.getElementById(id);
if (element.value.length >= 10) {
    alert("Mobile Number Should be in 10 digits only");
    element.value = "";
    window.setTimeout(() => element.focus(), 0);
    return false;
}

A timer with a count of 0 will run when the thread becomes idle. If that doesn't help, try the code (with the timer) in the `onblur` event instead.

Problem

When ever I do onchange event, its going inside that function its validating, But focus is not comming I am using `document.getElementById('controlid').focus();` I am using Mozilla Firefox and Google Chrome, in both its not working. I don't want any IE. Can any one tell me what could me the reason. Here's the code: ``` var mnumber = document.getElementById('mobileno').value; if(mnumber.length >=10) { alert("Mobile Number Should be in 10 digits only"); document.getElementById('mobileno').value = ""; document.getElementById('mobileno').focus(); return false; } ```

Original source

Related problems