If the ‘checked’ property indicates whether my checkbox was checked, what is the purpose of ‘value’?
value allows you to associate a label with your checkbox.
Let’s say you ask visitors to indicate their music preferences by checking one or more checkboxes. You can write code using the value property to save visitors’ selections as strings (rock, jazz, reggae) in your database collection.
Description
The value property is not related to whether the checkbox is checked or not. To determine the checked status of a checkbox, use the checked property.
The value property is used for storing a value that is associated with the checkbox.
It is not considered when evaluating the checkbox’s validity and it does not trigger an onChange event when the value is modified.
Even if a checkbox is connected to a dataset, its value property is not related to the value of the collection field it is connected to. The field’s value is determined by the checkbox’s checked property.
Note
Changing a checkbox’s value in code does not trigger an onChange event.
I’m aware of switches use, I’m not trying to set a value to or get a value from the switch, I’m only checking the switch (checked) status to run some code according to its status.
And in my case, the switch is connected to the dataset with a Boolean field, if the field value is true (checked), the switch is checked and vice versa, it was working fine for almost a week, but start returning these errors only today.
As a result of the error above, many other errors were risen, and to solve it as quick as possible I had to disconnect the switch from the dataset, and get its value directly from the database.
I don’t know what’s the reason of this, if the passed parameter is either true or false, then it’s Boolean, then why it’s returning an error.
I noticed that if you set Boolean in the dataset and it’s checked it’s returning “true” as it should; however, if it’s NOT checked it’s returning “undefined” why? why is it not returning “False”?
If it’s a Boolean it should return either “true” or “false”,
unless you add to the Boolean field a third option,
checked with green v = true
Checked with red x = false
Unchecked = undefined
Otherwise it confusing.
My code is working fine, because I use “if value is true”,
but you can’t use “if value is false” as it’s behaving strangely and not working as it should.
it seems you have to use undefined instead of false, which is odd for a Boolean
-EDIT=
Sorry for re-opening such an old thread, the date shows only the month and the year shows only when you hover over the date… so my bad, but the post still applies…
didn’t see the timeline to the right, looking at the posts themselves
I am not sure this is useful but, I thought I should mention it in a dataset, when adding Boolean values fields, you can set its default value, and if you don’t set this or manually toggle them all, then you get an undefined error.
Click the title of the dataset Boolean fields at the top then edit then default value to set it.
@CODE-NINJA’s advice is correct but I wanted to elaborate on this a bit more with a couple points as to why this is generally the correct way to do this, at least in JavaScript.
JS itself lacks strongly typed declarations and has no native notion of a ternary (or nullable) object. Instead every object can have either a value, null, or be undefined. If JS had type declarations we might be able to differentiate better using a nullable type like bool? which would explicitly declare that while a variable may have a boolean value it can also be null. But we don’t have this available to us.
Instead we leverage undefined, null, or both to represent certain edge cases. In this case entries in your database have 3 potential values for a boolean field:
true
false
Empty (aka undefined)
Empty is not false which is why we don’t want to assume and tell the frontend application that it’s false when it isn’t. Rather we tell frontend that it’s undefined which is an important piece of information. It lets us know that this particular field doesn’t have a value.
More practically then these values mean:
true = User checked the box
false = User unchecked the box
Empty (aka undefined) = User hasn’t engaged with the box
However in addition to doing the defensive programming that @CODE-NINJA mentioned above you can also do one of the following:
Make sure to set the value of this field whenever a new row is created
In the second option you’ll then get your queries returned so that the field is reported as the default value whenever it is empty. For example this database looks like: