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 values...

Greetings. The Wiki states: "Checkboxes and Radio Buttons For checkboxes and radio buttons, you must always specify a  value  attribute. For checkboxes, if the box is checked the attribute will be set to the value of the checkbox. If it is not checked, it will be set to "0". If you would like a checkbox to be checked by default, add the "checked=true" attribute. For example,  <input type='checkbox' name='attr_HasShield' value='1' checked='true' /> ." My question is, how can I make the "unchecked" something other than "0" ? I want to have the default value "-3" and the checked value "0" Thanks in advance.
1563424616

Edited 1563424723
vÍnce
Pro
Sheet Author
I think you'll need to use the checkbox as a toggle/flag for another input to get the values you want. example; Don Shield: <input type='checkbox' name='attr_HasShield_flag' value='1' checked /> Shield Mod: <input type='number' name='attr_HasShield' value='( @{HasShield_flag}*(-3) )' disabled />
Vince said: I think you'll need to use the checkbox as a toggle/flag for another input to get the values you want. example; Don Shield: <input type='checkbox' name='attr_HasShield_flag' value='1' checked /> Shield Mod: <input type='number' name='attr_HasShield' value='( @{HasShield_flag}*(-3) )' disabled /> Thanks Vince...I figured that would be the solution, and I am already working on it. I was hoping someone would have a much simpler solution  ;)
1563427381

Edited 1563429302
GiGs
Pro
Sheet Author
API Scripter
What do you want to use the value for? If it's used in a sheet worker, it doesnt matter what value you set it to. You can convert a 0 to -3, and a 1 to 0 within the sheet worker code. (This is the same method Vince suggests above, just doing the calculation inside the worker instead of using a second attribute.) If its in an autocalc field (or a macro), you can have a value of 3 for checked, and 0 for unchecked, then in the calculation subtract 3. This will give you the values you want.
GiGs said: What do you want to use the value for? If it's used in a sheet worker, it doesnt matter what value you set it to. You can convert a 0 to -3, and a 1 to 0 within the sheet worker code. (This is the same method Vince suggests above, just doing the calculation inside the worker instead of using a second attribute.) If its in an autocalc field (or a macro), you can have a value of 3 for checked, and 0 for unchecked, then in the calculation subtract 3. This will give you the values you want. Hi GiGs. I want to use the checkbox to indicate "Weapon Familiarity" as per the Hero System Rules. So, I want to have the weapon familiarity attribute value set at "-3" when unchecked, and "0" when checked. Here is an example piece of HTML defining the wf for Dagger <input class="field50 center" type="checkbox" name="attr_wfdagger" value="3" unchecked> Here is the Sheetworker I am using...     on('change:wfdagger sheet:opened', function () {         getAttrs(['wfdagger'], function(v) {             const wfdagger = v.wfdagger *1||0;             const dagger = Math.round(wfdagger - 3);             setAttrs({                  wfdagger: dagger             });         });     }); I think something like this will work...i just have to test it...
1563437842

Edited 1563571309
GiGs
Pro
Sheet Author
API Scripter
There's a problem there. You are setting the output attribute to the same as the input. Also, you can't set the checkbox to those values. To make this work, you are essentially following Vince's suggestion: You must have two attributes: one checkbox, and one number input. In sheetworker mode that is: <input type='checkbox' name='attr_wfdagger_flag' value='3' checked /> <input type='number' name='attr_wfdagger' value='' readonly/> In your script, you dont need the Math.round line you can just use const wfdagger = (v.wfdagger_flag *1||0) -3; Also since this is a checkbox, with predefined inputs: 0 and 3, you don;t need the ||0. That is for setting a default value when no number is recognised. For inputs, an empty input or text can mess up a sheet worker when you are expecting a number, so you do need to use that. But you don't for checkboxes. So you can simplify it: const wfdagger = +v.wfdagger_flag -3; And since this is essentially a single line calculation, you can dispense with both consts like so on('change:wfdagger_flag sheet:opened', function () { getAttrs(['wfdagger_flag'], function (v) { setAttrs({ wfdagger: +v.wfdagger_flag -3 }); }); }); If you have a lot of weapons and they are all named essentially the same way (wf and the name, with no spaces) you can use a Universal Sheet Worker . something like ['dagger', 'sword', 'mace'].forEach(weapon => { on(`change:wf${weapon}_flag sheet:opened`, function () { getAttrs([`wf${weapon}_flag`], function (v) { const weapon = +v[`wf${weapon}_flag`] - 3; setAttrs({ [`wf${weapon}`]: weapon }); }); }); }); The forEach loop here creates a separate sheet worker for each weapon you define in the array at the start, and replaces every instance of $(weapon} with the weapon's name.  This is one of the advantages of sheet workers over autocalc: it takes more code to set up a single sheet worker, but you can handle a lot of attributes with just a little bit extra code. All that said, you do need to decide what to do if people have skill levels in a particular weapon...
Hi GiGs...My last post was a garbled mess. Lack of sleep will do that to me...lol Looked at your advice and it makes sense. Will see how it goes. Cheers :)
GiGs & Vince...thanks for the advice. I worked it out using your suggestions, and with a small amount of tweaking the code worked exactly how I wanted it to. GiGs...I still haven't figured out the Loop, but my individual sheetworkers are all working perfectly. I will look at setting up the Loop later. Many thanks  :)