Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Checkboxes value not read until clicked

I have many sections that change the displayed text according to whether or not a particular checkbox is ticked. Here's the html for one: <input type="checkbox" style="width:18px; height:18px;" name="attr_scou1" value="1" />     <input class="sheet-tw sheet-tw0" type="checkbox" name="attr_scou1" value="0"><!-- TW0 -->     <input class="sheet-tw sheet-tw1" type="checkbox" name="attr_scou1" value="1"><!-- TW1 -->     <div class="sheet-twshow-TW0">Text A</div>     <div class="sheet-twshow-TW1">Text B</div> And here's the CSS: /*----------- Two Display Setup -------------*/ /*this hides the contents of each section by default*/ .charsheet div[class^="sheet-twshow"] {   display: none; } /*this shows the tw content when the appropriate input field is selected*/ .charsheet input.sheet-tw0:checked ~ div.sheet-twshow-TW0, .charsheet input.sheet-tw1:checked ~ div.sheet-twshow-TW1 {   display: block; } /*this hides the button*/ .charsheet input.sheet-tw {   width: 0px;   height: 0px;   opacity: 0;   display: none; } /*----------- End Two Display Setup -----------*/ It works fine except that in its initial state nothing is displayed. It should show Text A because the box is not checked. When the box is checked Text B appears and then when it's unchecked it shows Text A, so it's fine from then but it's annoying to have to click twice on every checkbox just to get the base state. Any ideas why it's doing this? Probably worth noting that these things are each themselves within a larger version – i.e. the whole thing is kept hidden unless a particular option is set in a <select> field. Could that be the cause?
1629475399
Kraynic
Pro
Sheet Author
You might try adding the bit to make it checked by default and see if that fixes things: <input type="checkbox" style="width:18px; height:18px;" name="attr_scou1" value="1" checked="true" />
1629476401
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
your problem is that you are attempting to use checkboxes like radios. When the html is initially created, the checkboxes are both created without :checked , even though the attribute itself is set to 0. This is because the attribute is actually stored on firebase, not in the html and the html updates its state when firebase changes. So, change your checkboxes to radios, and set one of them to be checked by default, and it will work appropriately.
Thanks for the tips. Unfortunately checked=true doesn't help because I need them to start unchecked. Tried checked="false", but that seems to do the same thing as "true". I don't think radio buttons will work either, because you can't uncheck a radio button. Unless I've misunderstood something (more than likely).
1629540753

Edited 1629540924
Oosh
Sheet Author
API Scripter
Unless I'm misreading your CSS, the only conditions where a text div is shown is when a checkbox is checked. If both are unchecked, there doesn't seem to be any other rule to override the display: none on both Text A and Text B. If you want Text A displayed when the box is unchecked .... what is the checkbox tw0 for? Or am I misunderstanding?
1629548230
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
As Oosh says, you've set up your checkboxes so that they function as radio buttons, except for when a sheet is first loaded. And that first load issue is only because of an initial desync between the html and firebase; the actual value of the attribute if you call it via a sheetworker or an attribute call in chat is going to be 0 . And, barring API/sheet changes to the attribute or another version of it elsewhere on the sheet, initial sheet creation is the only time both of those checkboxes will be unchecked. I think we might be having an x-y problem. What is your end goal with this set up?
Perhaps if I try to explain the code in more detail (it's not mine - got it from CSS Wizardry, but can't seem to find it again for a link). 'Text A' and 'Text B' are both hidden by default. Setting the value of the attr to 0 reveals what's in  <div class="sheet-twshow-TW0"> Setting the value of the attr to 1 reveals what's in  <div class="sheet-twshow-TW1"> This system can work with any numerical attribute - it can go above 1 and even come from a <select> input, but because my need here is only binary I went with a checkbox. Click on, click off. This is all working perfectly, except that before the box is clicked for the first time it displays nothing at all. It's as though the default value of the attr is being read as null, not even 0. After a bit more testing, I've now found that as soon as any checkbox is clicked the problem resolves immediately for all the others. When the page is refreshed the problem comes back, but only for those sections where the corresponding checkbox has never been clicked (for that character sheet). Weird(?). So my end goal is for it to work as it is (checked = display 'Text A', unchecked = display 'Text B') but for the sheet to know that the value of a checkbox that has never been checked is 0.
1629555923

Edited 1629572528
GiGs
Pro
Sheet Author
API Scripter
Why does your original code have three checkboxes, two of which are identical? <input type="checkbox" style="width:18px; height:18px;" name="attr_scou1" value="1" />     <input class="sheet-tw sheet-tw0" type="checkbox" name="attr_scou1" value="0"><!-- TW0 -->     <input class="sheet-tw sheet-tw1" type="checkbox" name="attr_scou1" value="1"><!-- TW1 --> The first and third here have the same attribute name and same value. Correct me if I'm wrong: you want one specific div to show on page load, and you want a different div to show when a specific checkbox is checked? If so, a better way to handle this would be to use just a single checkbox:     <input type="checkbox" class="sheet-tw sheet-tw1 sheet-18px" name="attr_scou1" value="1" />     <div class="sheet-twshow-TW0">Text A</div>     <div class="sheet-twshow-TW1">Text B</div> and the CSS something like: /*----------- Two Display Setup -------------*/ /*this hides the contents of each section by default*/ .charsheet div[class^="sheet-twshow"] {   display: none; } /*this shows the tw content when the appropriate input field is selected*/ .charsheet input.sheet-tw1: not(:checked) ~ div.sheet-twshow-TW0, .charsheet input.sheet-tw1:checked ~ div.sheet-twshow-TW1 {   display: block; } /*this hides the button*/ .charsheet input.sheet-tw {   width: 0px;   height: 0px;   opacity: 0;   display: none; } .charsheet input.sheet-18px {     width:18px;     height:18px; } /*----------- End Two Display Setup -----------*/ The part that hides the input is a bit weird - makes it very hard to test, so I removed that.
1629592157

Edited 1629592250
Oosh
Sheet Author
API Scripter
Rich K. said: Setting the value of the attr to 0 reveals what's in  <div class="sheet-twshow-TW0"> Setting the value of the attr to 1 reveals what's in  <div class="sheet-twshow-TW1"> Just to address this part, and why you're not getting expected results - that isn't what the original CSS is doing. .charsheet input.sheet-tw0:checked ~ div.sheet-twshow-TW0, .charsheet input.sheet-tw1:checked ~ div.sheet-twshow-TW1 { These CSS rules are just looking at the HTML to see if the checkboxes are checked - they're not actually checking the attribute values at all. In this case the Roll20 attribute name & value are irrelevant (for the purposes of the CSS, anyway) - it's a pure CSS/HTML check of the 'checked' HTML attribute of the checkbox <input>. If you're wanting to use the Roll20 attribute values, you need to use the [value] CSS selector - see the "tabs" example on CSS Wizardry for an example. GiGs & Scott are far more experienced than I am, but as far as I can tell only <input type="hidden"> elements respond to attribute changes properly. The others have a static HTML value attribute, no matter what happens on the character sheet. This may be on the Wiki, but I don't remember reading it.
1629597711
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ok, there's several things going on here, and to understand what's happening we first need to dive into how the Roll20 character sheet system works. So, let's take a look. NOTE, these are observational and not based on knowledge of how Roll20 has actually set up the networking and system. So, first of all, there's three locations for attribute data in the system. The HTML defined attributes The attributes stored on Firebase The attributes on the attributes and abilities tab Aside from the layout, the HTML defines what the default value of the attributes is the first time that the character sheet is loaded. When a character sheet is first opened, Firebase only updates the HTML for a given attribute if the value it has stored is different from the default value. This check likely goes something like: HTML: "Firebase, here's what the attribute starts as" FB: "Thanks" whispers to self, I already know that, you told me last week! Additionally, the attributes and abilities tab only adds an attribute to the list visible there when the value is changed from the default. So, the exchange of information looks something like this: HTML <==>> Firebase <==> AA tab Ok, so we've got 3 areas where the attribute values are stored/displayed and 2 where they can be edited. However, the exchange of information is not always the same (hence the funky arrows between FB and HTML). When a sheet is first opened, Firebase only gives it the information that has previously been set by a user. The HTML then reacts to those changes appropriately (like checking checkboxes for valid values). Now, let's bring this around to your specific issue. If the checkbox attribute's value has never been changed, Firebase does not tell the HTML that the value is a given value as it assumes that the HTML already knows what the default value is supposed to be and has reacted accordingly. Now, when a change is made through the html interface (say clicking a checkbox), the html updates it's references to the firebase values and gets the current values for everything. In your case this then triggers the HTML to realize that the checkbox is set to 0 and updates the checked state of that checkbox. So, that's what's going on with why it displays incorrectly the first time you open the sheet with it unchanged from its default value (if you ever toggled it back and forth, it would display correctly). Another problem for your code though is that you've setup a checkbox so that it is working against how Roll20 character sheet checkboxes work. In HTML, checkboxes have a default checked value of "on". In Roll20 character sheets, they also have a default unchecked value, which is 0. This is set by the backend of Roll20 and is untouchable by character sheet code. What this means for your code, is that your 0-input is trying to be both checked and unchecked. And finally, as I mentioned in an earlier post, your current set up is attempting to use checkboxes as radios. By which, I mean you have multiple inputs for a single attribute with separate checked-values and one of them is supposed to always be checked leaving the other(s) unchecked. That's pretty much the definition of a set of radio buttons. Now, as GiGs rightly pointed out, your current setup really only needs a single checkbox, with a styling state based on the checkbox being unchecked, and one for it being checked. However, if you are planning to add more states or you want to display an "unchecked" input at each display mode, then you'd need a radio. 
Thanks Scott, that's really interesting. I'm a bit baffled as to why the checkbox html can't be told to default to 0 and thus agree with Roll20, but if it's just fundamental to how html works then hey ho.
Thanks GiGs Why does your original code have three checkboxes, two of which are identical? Couldn't tell you, not my design – got it from CSS Wizardry and all I can be 100% sure of is it does what I want it to do, except the issue I raised with this post. But your version fixes (circumnavigates?) the problem, so thanks very much!
1629638271
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Rich K. said: Thanks Scott, that's really interesting. I'm a bit baffled as to why the checkbox html can't be told to default to 0 and thus agree with Roll20, but if it's just fundamental to how html works then hey ho. You can though. Simply designate the check box that you want checked by default as having the checked property in the html.
Ooh, yeah that works too. Cool, much appreciated.