Alpine: Binding a value on checkboxes = checked by default

Created on 5 May 2020  路  10Comments  路  Source: alpinejs/alpine

Is there a reason why x-bind:value on checkboxes check themselves by default?

I think this may be a bug. See CodePen below as an example.

https://codepen.io/richardcool/pen/eYpyMWB

bug

Most helpful comment

If the "checked" attribute is the one that determines whether the input value is posted with the form, then setting the value via x-bind shouldn't affect the checked state. Instead it should be possible to use x-bind:checked to modify the default, and sub-sequent, checked states.

All 10 comments

You had the bind value for the checkbox set to be a truthy value (in your case it's license.title) that causes the checkbox to be checked

It has nothing to do with alpinejs.
You had the bind value for the checkbox set to be a truthy value (in your case it's license.title) that causes the checkbox to be checked

But if I hardcode the value value="broadcast" then it works fine?

I'm going to passing the inputs to a POST request so need the values to be useful...

Not really. It has something to do with Alpine. Alpine does bind checked depending on the value attribute. Probably for some use cases Caleb decided this approach. Not sure what is the real reason but, FYI.

The value attribute defines a value which is sent by a POST request (i.e. You have an HTML form submitted to a server). Now the server gets the name (if defined) and the value.

But Alpine converts this value to "checked" right here

Which uses !! bang bang to convert string to boolean and apply checked based on this value.

Remove x-bind:value and instead set/unset the value of the checkbox using the @click event on the input

@click="$event.target.value = $event.target.checked ? license.title : ''"

Sorry for that uneducated comment @richgcook, what @muzafferdede said was correct. Use @jreviews workaround for now

Remove x-bind:value and instead set/unset the value of the checkbox using the @click event on the input

@click="$event.target.value = $event.target.checked ? license.title : ''"

Nice workaround, not sure how we might go about fixing this use case without a breaking change 馃

If the "checked" attribute is the one that determines whether the input value is posted with the form, then setting the value via x-bind shouldn't affect the checked state. Instead it should be possible to use x-bind:checked to modify the default, and sub-sequent, checked states.

One for Caleb but I would classify it as a bug so fixable as part of a minor release.
I think the code works like that because it's used by x-model as well. We should probably do the same thing we use for radio buttons.

@richgcook or anyone else want to create a PR with a failing test for this case?

@richgcook or anyone else want to create a PR with a failing test for this case?

Sure

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bep picture bep  路  4Comments

Calinou picture Calinou  路  4Comments

imliam picture imliam  路  5Comments

zaydek picture zaydek  路  3Comments

maxsite picture maxsite  路  4Comments