Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onautocomplete triggered on checkbox toggle #49

Open
fini opened this issue May 30, 2022 · 2 comments
Open

onautocomplete triggered on checkbox toggle #49

fini opened this issue May 30, 2022 · 2 comments

Comments

@fini
Copy link

fini commented May 30, 2022

Describe the bug
For some reason the onautocomplete triggered whenever I clicked on a checkbox in my form, I've had to disable it for now.

Expected behavior
No event triggered on checkbox toggle

Desktop (please complete the following information):
MacOS
Chrome Version 102.0.5005.61 (Official Build) (arm64)

@berguntinfor
Copy link

berguntinfor commented Aug 18, 2023

Same issue. I think it's related to bootstrap's styles in my case. Solved after deleting form-check-input class

[Edit]
Not solved - Issue persist and checkboxes have no value on form submit

@attodorov
Copy link

attodorov commented Aug 28, 2023

The issue is not related to boostrap or to css classes in general. It's because of this line in the onInput event handler:

('insertReplacementText' === event.inputType || !('data' in event)) ?

This is for the input event, which is registered like this:

document.addEventListener('input', onInput, true);

For radio buttons and checkboxes (the issue is also applicable to radios), there's no 'data' property in the event object, therefore it assumes autocomplete. That's because "data" holds characters entered, and neither checkboxes, nor radio buttons have this. I think in order to make this more resilient, instead of checking for data in event, the code should check if there was a previous focus or a mousedown (as in ... right before the input event, in a small time interval). This will work for both inputs ad well as radio buttons and checkboxes, when they are autofilled.

let lastClickTime = 0;

document.addEventListener('mousedown', function() {
lastClickTime = Date.now();
});

function onInput(event) {
if (event.target.tagName === 'INPUT' && (event.target.type === 'checkbox' || event.target.type === 'radio')) {
if (Date.now() - lastClickTime < 150) {
cancelAutocomplete(event.target); // It was a manual interaction
} else {
autocomplete(event.target); // Likely autofill for checkbox/radio
}
} else if ('insertReplacementText' === event.inputType || !('data' in event)) {
autocomplete(event.target);
} else {
cancelAutocomplete(event.target);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants