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

Checkbox Troubles

1723421491
Wysemoor Games
Pro
Marketplace Creator
I am trying to automatically check and uncheck checkboxes on my character sheet when certain values are set. I know how to call the change on the dropdown that I'm watching, but I cannot get checkboxes to respond properly. This is what my checkbox looks like <input type="checkbox" id="MinA" name="attr_MinA" value="0" unchecked /> This is what I'm trying to do.     on("change:Wounds", function() {         getAttrs(["Wounds"], function(values){             let Mor = values.Mortal;             if (Mor > 0)             {                   setAttrs({"MinA":1});             }         });     }); I have other workers that work fine, setting numeric and text values on the sheet. And the function calls when I change the value of Wounds. I just can't find any clear documentation on this. I'm happy to have 0 be unchecked and 1 be checked. As well, when I try to manually change the checkbox and get it to trigger a change event, it only does so on the first change, and never again after. 
1723426455

Edited 1723578679
GiGs
Pro
Sheet Author
API Scripter
In your checkbox, you have value="0". For a checkbox, value works differently to other inputs. With a checkbox, the value is "set to this value when the checkbox is checked". So you should do <input type="checkbox" id="MinA" name="attr_MinA" value="1" > It will have a value of 0 when unchecked. You dont need to include the unchecked keyward, because by default it is unchecked. You also don't need to include the / ending in inputs. In your worker, when you get it working, there is no way to uncheck the checkbox. Let's say you set Mortal to something. The checkbox is checked. Then you set Mortal to 0. The checkbox remains checked, because there is no way to uncheck it. If this is not desirable,do something like this: on("change:Wounds", function() {         getAttrs(["Wounds"], function(values){             let Mor = +values.Mortal || 0; // this makes sure it is a number             if (Mor > 0)             {                   setAttrs({MinA:1});             } else { setAttrs({MinA:0}); }         });     }); Or, more compactly: on("change:Wounds", function() {         getAttrs(["Wounds"], function(values){             let Mor = +values.Mortal || 0;             setAttrs({ MinA: Mor ? 1 : 0 });         });     }); This version uses a ternary operator (a one-line if statement that chooses between two values, here 1 or 0), and takes advantage of the way if statements work in javascript - they check for "truthy" values, so if(Mor) is the same as if(Mor > 0). Technically, it's not exactly the same - if Mor is below 0, but not exactly 0, it will be read as truthy and given a a value of 1. That line maybe should be: setAttrs({ MinA: Mor > 0 ? 1 : 0 });
1723452021
Wysemoor Games
Pro
Marketplace Creator
Thanks, looks like that worked!