Svelte: Conditionally add a boolean attribute

Created on 9 Nov 2017  路  4Comments  路  Source: sveltejs/svelte

Hi, I'm just wondering whether it's currently possible in Svelte to conditionally append a boolean attribute to a node, ie a value-less attribute whose existence/absence alone controls the behaviour of the node.

Example:

<select class="foo" disabled>
 ...
</select>

If-blocks within the tag don't seem to be allowed in this case; ternary expressions aren't being evaluated, so it seems the only possibility right now would be to wrap the entire node in an if-block. Or am I missing something?

Most helpful comment

Thanks a lot and excuse the GH issue, I don't know how I missed that!

All 4 comments

For boolean attributes like disabled, you can just do disabled="{{whatever}}" and the disabled will be set or unset based on whatever. https://svelte.technology/guide#tags

Thanks a lot and excuse the GH issue, I don't know how I missed that!

Is this obsolete syntax? i.e. I know that svelte used to use double curly braces, but now use single braces. For me, this is relevant for field validation, where I want to conditionally add or not add the required attribute to input fields.

@johndeighan yes. It's from 2017 (Svelte v2).

The syntax would now be:

<select class="foo" disabled={yourBoolean}>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AntoninBeaufort picture AntoninBeaufort  路  3Comments

davidcallanan picture davidcallanan  路  3Comments

st-schneider picture st-schneider  路  3Comments

noypiscripter picture noypiscripter  路  3Comments

rob-balfre picture rob-balfre  路  3Comments