The code below displays two text input fields and either creates either an enabled or disabled button depending on whether. There is also a Javascript timer to enable the button when people complete the form.
With dynamic button creation and code on a loop to enable or disable the button when the form is populated with autocomplete the button does not enable until the web page is clicked on anywhere or the user enters a character into the form. This confuses people as the form is complete but the button is disabled.
It seems like the form is not actually completed, just that Chrome/Firefox show the fields as complete but do not actually do so until the user interacts with the page.
How do I get the button to enable when the form is completed automatically with autocomplete?
<input type='text' id='username' autofocus />
<input type='password' id='password' />
<script type="text/javascript">
if (document.getElementById('username').value != '' && document.getElementById('password').value != '') {
document.write('<input type="submit" value="Login" />');
} else {
document.write('<input type="submit" disabled="disabled" value="Login" />');
}
</script>
For reference this is the code run on a timer used to check is the form is complete.
window.onload = function () {
var check_interval = 250;
window.setInterval( function() {
if (document.getElementById('username').value != '' && document.getElementById('password').value != '') {
document.getElementById('button').removeAttribute('disabled');
} else {
document.getElementById('button').setAttribute('disabled', 'disabled');
}
}, check_interval);
};
Edit: onChange is not a good solution for this problem as autocomplete does not generate events across all browsers. There is some information in the answer to this question. onChange does not work well with our forms anyway, one form has a user set a password where the button enables only when the password is deemed strong enough by zxcvbn lib. Polling works well for that, onChange only seems to kick in once the user has moved to another field.