I am trying to set up a simple system that lets the user choose one of two options or neither of them. The options are both stored on the same complex object, which holds all the settings (not just those two).
I already looked at similar questions like this one: How to do something when an attribute changes in svelte
But unfortunately they don't work for my case - they react to changes of the entire object instead of just the one attribute. And even using the &&
syntax, as described below, works almost, but not fully. It seems perfect, as it only reacts to the value being truthy, which is what I need, but it only works one way.
Here is a simplified version of my problem:
<script>
let object = {a: false, b: false};
$: object.a && aActivated();
$: object.b && bActivated();
function aActivated(){
object.b = false;
}
function bActivated(){
object.a = false;
}
</script>
<label for="a">a</label>
<input type="checkbox" bind:checked={object.a} id="a">
<label for="b">b</label>
<input type="checkbox" bind:checked={object.b} id="b">
As you can see when you run this, a is able to deactivate b, but not the other way around.
I also tried replacing &&
with ||
and ,
, but in both cases the functions would run whenever anything on the object changed, making it impossible to select either one of the options.
And no, I cannot switch to using radio buttons, because I want the users to be able to deselect both options, too.