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

Two-way binding select does not allow for dynamic multiple attribute #1270

Closed
rspieker opened this issue Mar 22, 2018 · 5 comments · Fixed by #1313
Closed

Two-way binding select does not allow for dynamic multiple attribute #1270

rspieker opened this issue Mar 22, 2018 · 5 comments · Fixed by #1313
Labels

Comments

@rspieker
Copy link

The two-way binding on multiple select elements don't take a dynamically set multiple-attribute into consideration.
Maybe this is as intended, in which case it appears to be missing from the documentation (or I simply overlooked it).

A simple REPL to demonstrate the behavior.

Short examples:

  • Works: <select bind:value multiple>
  • Doesn't: <select bind:value :multiple>
  • Doesn't:<select bind:value multiple="{{ multiple }}">

As for browsers, the behavior is consistent across browsers, tested in Firefox 60, Brave 0.21, Electron 1.8.3 (Chromium 59)

@Rich-Harris
Copy link
Member

Thanks — opened #1313. Things get complex fast if you allow multiple to be dynamic on a bound <select> element, so realistically the best way to handle this is as a compiler error. If you need to be able to toggle between the two states it's probably best done with an if block.

@Rich-Harris Rich-Harris added the bug label Apr 4, 2018
@rspieker
Copy link
Author

rspieker commented Apr 4, 2018

Thanks for your response. These kind of details have the tendency to be much harder to implement than just a simple workaround. A compiler error will help users to catch on to what is happening.

Rich-Harris added a commit that referenced this issue Apr 4, 2018
fail validation if bound <select> has dynamic multiple attribute
@Rich-Harris
Copy link
Member

Released 1.60.2 with this change

@blujedis
Copy link

2023...just ran across this. Thought I'd post that @Rich-Harris point to use if statement is still best path as near as I can tell.

If you use bind:value for example you'll want to set multiple without a value. Meaning even multiple={true} will complain. You will want <select bind:value multiple></select>

@matths
Copy link

matths commented Jul 25, 2024

Using Svelte 4.2.18 I did it like this:

inside my script block:

  export let multiple: boolean;
  export let ref: HTMLSelectElement;

  $: {
    if (multiple) {
      ref && ref.setAttribute('multiple', '');
    } else {
      ref && ref.removeAttribute('multiple');
    }
  }

inside the markup section

<select bind:this="{ref}" bind:value="{value}">
   ...
</select>

...which is exactly what I would have Svelte expected to do. And I don't get why it doesn't and comes up with a compiler error instead, but this is my workaround. Maybe it is of some use for someone else.

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

Successfully merging a pull request may close this issue.

4 participants