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

For a selector with the multiple attribute, when multiple values are selected, only one value remains. #12

Closed
shoysenberg opened this issue Jan 23, 2025 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@shoysenberg
Copy link

Hello Dima! I'm developing a filter, and I chose yours as a library for rendering a beautiful selection. You probably designed it for the filter, which is very convenient! But there was a problem with selecting two values in the selector, I set the EventListener to change, and only the first selected value is displayed in the selector, although the native selector has the multiple attribute.

@William-H-M
Copy link

Hi, do you have any working example?
Currently, I do something like this on a helper like file

defaultSelect.addEventListener('change', function (e) {
    return getSelectedValues();
});

getSelectedValues() {
    let values = Array.from(defaultSelect.options)
        .filter((x) => x.selected && x.value)
        .map((x) => x.value);
    if (!values.length) {
        return null;
    }
    if (defaultSelect.multiple) {
        return values;
    }
    return values[0];
}

@afonja14755
Copy link
Owner

Hello @shoysenberg, please provide some example.

@afonja14755 afonja14755 self-assigned this Feb 17, 2025
@afonja14755 afonja14755 added the question Further information is requested label Feb 17, 2025
@afonja14755
Copy link
Owner

@William-H-M, @shoysenberg.

If I understand the essence of the question correctly, unfortunately, this is standard behavior even in a native select. First and foremost, I strongly recommend passing form data using new FormData, but if it happens that you have to extract and handle them manually, then I added the method getSelectedValues in latest release for you, which returns an array of the values of the selected options.

@William-H-M
Copy link

@afonja14755 Yeah, I was just answering the question, is this getSelectedValues something that shouldn't affect the behavior of the native select.
The way I did it is because I'm using a vue component, so the model does indeed return something an API could accept.

But is nice to have something the library can handle itself, thanks

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

No branches or pull requests

3 participants