Skip to content

Unable to add attribute without a value using templates #301

@ccampbell

Description

@ccampbell

It is entirely possible I am missing something very obvious, but I can’t seem to figure out how to render a <select> menu and add a selected attribute for the selected option.

I tried using this but it gives a syntax error

<option value="b"{{#if (val == 'b') }} selected{{/if}}>b</option>

https://svelte.technology/repl/?version=1.6.11&gist=dbf07e495a3c768a89a6de924c44ead9


I also tried using a ternary as described in #259, but that too gives a syntax error

<option value="b"{{ (val == 'b') ? ' selected' : '' }}>b</option>

The only way I could get it to work was doing this:

<option value="b" selected="{{ (val == 'b') ? ' selected' : '' }}">b</option>

https://svelte.technology/repl/?version=1.6.11&gist=4c44e8756ae112646cc1150077a555d0

That is not ideal because it generates a bunch of unnecessary code to have to repeat the selected= for every option even if they are not selected (although I suppose gzip will take care of some of that).

Is there a recommended/better way to do this?

Thanks so much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions