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

get name of input from name of checkbox, and change input

1486279828
GiGs
Pro
Sheet Author
API Scripter
I have some inputs holding the value of skills, named like "Sword", "Awareness", "Orate", etc. For each one there is a checkbox, named "Sword_check", "Awareness_check", "Orate_check", etc. What I'd like to do is this: in a sheetworker, when the player clicks the check box, increase the value of the related skill by 1. Is there a way to grab the name of the touched control, remove "_check", and use the resulting name to access the control? (I know how to string trim, but not the first and last parts)
1486294217

Edited 1486295041
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
It seems to me that what you really want to use is a button instead of a checkbox.  Nonetheless, you could have a sheetworker routine that "on change" of any of the _check values, look in the eventinfo for which one changed, if it has a non-zero value you read it's corresponding numerical value, add one, then write the new value and write a zero to the _check (so it is ready to be used again). 
1486294668
Kryx
Pro
Sheet Author
API Scripter
const skills = ['sword', 'awareness', 'orate']; const incrementValue = (skill) => { getAttrs([skill], function(values) { setAttrs({ skill: parseInt(values.skill, 10) + 1 }); }); }; skills.forEach((skill) => { on(`change:${skill}_check`, () => { incrementValue(skill); }); });
1486309400
GiGs
Pro
Sheet Author
API Scripter
Kryx said: skills.forEach((skill) => { on(`change:${skill}_check`, () => { incrementValue(skill); }); }); Whoa! You can do that? I had no idea you could loop to set up a on:change event. Chris D. said: It seems to me that what you really want to use is a button instead of a checkbox.  Nonetheless, you could have a sheetworker routine that "on change" of any of the _check values, look in the eventinfo for which one changed, if it has a non-zero value you read it's corresponding numerical value, add one, then write the new value and write a zero to the _check (so it is ready to be used again).  Hmmm. i need to look into eventinfo. About the button: how would that work differently from using a checkbox and change evet? I am not sure how to launch sheetworkers via buttons.
1486332117
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
Buttons can't be done with just sheetworkers, they work with the API, so if you have a PRO account, you can use buttons with the api.  If you don't have a PRO account and want to do it solely with sheetworkers, I think you are on the right track with using a checkbox. I think that somewhere in the CSS wizardry thread there may be something in how to make a checkbox look like a button.  Yes, I think Kyrx's code would probably work, but as you note, it uses some weird looking code that you might have to think through to really understand. If you want something really straightforward and easy to understand, just have one big long on:change string that lists all the checkboxes. the  wiki page explains eventinfo pretty well.  on("change:strength change:StrengthMod change:StrengthOtherThing", function(eventInfo) { // eventInfo.sourceAttribute is the full name (including repeating ID) of the attribute that originally triggered this event }