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

[Sheet Workers] Populating textarea using dropdown box in repeating section

How can I change the text in a textarea depending on the selected value of a dropdown?  I want a description of each choice to appear next to the dropdown in each repeating section.  Here's my code: <div class='sheet-damagetype-container'> <fieldset class="repeating_damage"> <select name="attr_damagetype" class="sheet-damagetype"> <option hidden disabled selected value>Damage</option> <option value="d4">d4</option> <option value="d6">d6</option> <option value="d8">d8</option> <option value="d10">d10</option> <option value="d12">d12</option> </select> <textarea name="attr_damagetext"></textarea> </fieldset> </div> And the script: <script type="text/worker"> on("change:damagetype", function(eventInfo) {      var damage = eventInfo.newValue;     var text = '';      switch (damage) { case "d4": text = "d4 damage yada yada"; break; case "d6": text = "d6 damage yada yada"; break;   case "d8": text = "d8 damage yada yada"; break; case "d10": text = "d10 damage yada yada"; break;   case "d12": text = "d12 damage yada yada"; break;   default: text = '' break;      }   setAttrs({ repeating_damage_damagetext: text }); }); </script
1535501815

Edited 1535501856
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
When setting an attribute in a repeating section you have to specify the repeating row you are setting. Something like this should work: <script type="text/worker"> on("change:damagetype", function(eventInfo) {      let damage = eventInfo.newValue,     text = '',          sectionAttr = eventInfo.sourceAttribute;      switch (damage) { case "d4": text = "d4 damage yada yada"; break; case "d6": text = "d6 damage yada yada"; break;   case "d8": text = "d8 damage yada yada"; break; case "d10": text = "d10 damage yada yada"; break;   case "d12": text = "d12 damage yada yada"; break;   default: text = '' break;      }   setAttrs({ `${sectionAttr.replace(/damagetype/,'damagetext')}`: text }); }); </script
1535503769
GiGs
Pro
Sheet Author
API Scripter
Since its a repeating section, you need to grab that in the on:change on("change:repeating_damage:damagetype", etc
1535504067
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
ah, good catch GG.
1535504089

Edited 1535504148
GiGs
Pro
Sheet Author
API Scripter
I would have done the script the more familiar (to me) way: on ( "change:repeating_damage:damagetype" , function () { getAttrs ([ "repeating_damage_damagetype" ], function (values) { let damage = values.damagetype; let text = ''; switch (damage) { // your damage code here } setAttrs({ repeating_damage_damagetext: text }); }); });
1535505272
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Aye, but in this case, you can avoid having to go through a getAttrs async round because all the information is contained in the change event (at least as long as this isn't being caused by a token bubble being changed). Avoiding the getAttrs makes it quicker.
Tried both and didn't work.  Will the textarea automatically update just by changing the attribute?
1535509962
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Heh, looks like we both had errors in ours. I've changed some of GG's and incorporated some of mine below: <script type="text/worker"> on("change:repeating_damage", function(eventInfo) {      let damage = eventInfo.newValue,     text = '',          sectionAttr = eventInfo.sourceAttribute;      switch (damage) { case "d4": text = "d4 damage yada yada"; break; case "d6": text = "d6 damage yada yada"; break;   case "d8": text = "d8 damage yada yada"; break; case "d10": text = "d10 damage yada yada"; break;   case "d12": text = "d12 damage yada yada"; break;   default: text = '' break;      }   setAttrs({ `${sectionAttr.replace(/damagetype/,'damagetext')}`: text }); }); </script
Not sure what's going on but still not working.&nbsp; Here's the sheet. <a href="https://gist.github.com/littlebigbucky/abba7d17d6fe63753d371a3f1b3c7379" rel="nofollow">https://gist.github.com/littlebigbucky/abba7d17d6fe63753d371a3f1b3c7379</a> I initially tried to copy the way the wiki codes it.&nbsp; But that doesn't seem to work either. on ( "change:repeating_spells:spelllevel" , function () { getAttrs ([ "repeating_spells_SpellLevel" , "repeating_spells_SpellName" ], function (values) { setAttrs ({ repeating_spells_SpellDamage: Math. floor (values.repeating_spells_SpellLevel / 2 ) + 10 }); }); });
Created new game with no scripts and still doesn't work.&nbsp; Changed the textarea to input but that didn't help &lt;input type='text' name="attr_damagetext"&gt;&lt;/input&gt;&nbsp;
1535544488
GiGs
Pro
Sheet Author
API Scripter
You dont properly close the script block &lt;/script should be &lt;/script&gt;
1535546338
Jakob
Sheet Author
API Scripter
Just another stab here, Scott's code is missing brackets around the property: template literals don't work as object keys without wrapping. setAttrs({ [sectionAttr.replace(/damagetype/,'damagetext')]: text });
Progress!&nbsp; Adding the brackets like Jakob said makes the text flash in the textbox for a second but then it disappears.
Interestingly if I make it a span the text stays.
1535554524
Jakob
Sheet Author
API Scripter
Michael D. said: Interestingly if I make it a span the text stays. OK, try this: replace the event string in Scott's solution by making it more specific as follows: on("change:repeating_damage:damagetype", function(eventInfo) {
Thanks that did the trick.
1535578666
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
yep, or could have changed the setAttrs to be silent: setAttrs({ [sectionAttr.replace(/damagetype/,'damagetext')]: text },{silent:true});
1535612253
Jakob
Sheet Author
API Scripter
Scott C. said: yep, or could have changed the setAttrs to be silent: setAttrs({ [sectionAttr.replace(/damagetype/,'damagetext')]: text },{silent:true}); I actually wouldn't recommend doing that, since that would cause this script to misbehave again if OP adds other attributes to this repeating section.
1535633473
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Fair point. It would need logic to figure out what it should do.