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

Can a select tag contain 2 separate values?

1726969856
Mago
Sheet Author
hello forum, im struggling with a situation and i need some ideas the problem i have is that i need 2 separate values stored inside a <select><option> tag within a repeating section now here is a much simplified version of what im dealing with <input name="attr_base" type="number" value="3" /> <fieldset name="attr_repeating_powers>     <select name="attr_modifier">         <option value="*3" >x3</option> /*roll needs 2d10*/         <option value="*2" >x2</option> /*roll needs 1d10*/         <option value="*1" >x1</option> /*roll needs 1d5*/      </select>      <button name="roll_test" type="roll" value="[[ (@{base}@{modifier}) + @{dice} ]]" </button> </fieldset> Now the issue is that the @{dice} also changes based on the modifier selected i could insert the dice into the options value however i need to keep the values separated because Base+Modifier is used by other macros inside the same fieldset i tried to make a scriptworker to change the dice but it gets messy because the attributes are inside a repeating section any insights are appreciated
1726973761

Edited 1726973803
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
A sheetworker is your only option for this. If you give the full setup of the section, it should be pretty easy to demo for you.
1727147186
Mago
Sheet Author
hello scott, this is so far what i got <input style="width:5%; font-weight:bold" name="attr_psyrating" type="number" value="0"/> <fieldset class="repeating_psypowers3" name="attr_psypowers3">                         <select name="attr_epr3" style="width:21%">                             <option disabled style="background:gray;font-weight:bold;color:white;">Psy Strength</option>                             <option value="(ceil(@{psyrating}/2))">Fettered</option>                             <option selected value="@{psyrating}">Unfettered</option>                             <option value="(@{psyrating} + 3)">Push</option>                         </select>                         <input type="hidden" name="attr_psydice3" value=""/>                 </fieldset> ---------------------sheet worker----------------------------- on("change:repeating_psypowers3 sheet:opened", function() {   getAttrs(["repeating_psypowers3_$0_epr3","psyrating"], function(values) {       var epr = parseInt(values["repeating_psypowers3_$0_epr3"]);       var psyrating = parseInt(values["psyrating"]);       var output = "";       console.log(values["repeating_psypowers3_$0_epr3"],psyrating);       if (psyrating > epr) {         output = "1d100cs101cf>91";       }       else if (psyrating == epr) {         output = "1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99cs100cf>91";       }       else {         output = "1d100cs>0cf>91";       }       setAttrs({         "repeating_psypowers3_$0_psydice3": output     });   }); }); so far this script works, sort of..., but it always gives me the else output, pretty sure there is something in the repeating field call that is not working
1727180662

Edited 1727194490
GiGs
Pro
Sheet Author
API Scripter
If you're always getting the else result, that means the if statement isn't working - it's structure means that you'll get the else result when that comparison is not right. The first thing I'd check: you dont want to be using this syntax in a sheet worker: repeating_psypowers3_$0_epr3 - thats for macros. The proper syntax would be  repeating_psypowers3_epr3 if you know it's just a single row, or repeating_psypowers3_ROW-ID_epr3 , where ROW-ID correctly identifies one row out of several, but this requires the getSectionIDs function to be used first. Judging by the structure of your sheet worker, it's probably the second. Does this give the correct response or is it undefined: console.log(values["repeating_psypowers3_$0_epr3"],psyrating);
1727182981

Edited 1727183331
GiGs
Pro
Sheet Author
API Scripter
Here's probably how I would create the sheet worker (well, it wouldn't be exactly like this, but it's close enough). Ask if you have any questions! on ( "change:repeating_psypowers3:epr3 change:psyrating sheet:opened" , function () {     getSectionIDs ( 'repeating_psypowers3' , function ( ids ) {         const fields = [];         ids . forEach ( function ( id ) { /* ids is an array that contains every row id in the section */             fields . push ( `repeating_psypowers3_ ${ id } _epr3` );         });         getAttrs ([... fields , "psyrating" ], function ( values ) { /* the ... spread operator takes an array here and breaks it out into each item */             var psyrating = parseInt ( values [ "psyrating" ]);             var output = {};             ids . forEach ( function ( id ) { /* we use ids to create the fields array above, for getSectionIDs, and we loop through it again here */                 var epr = parseInt ( values [ `repeating_psypowers3_ ${ id } _epr3` ]);                 if ( psyrating > epr ) {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs101cf>91" ;                 } else if ( psyrating == epr ) {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99cs100cf>91" ;                 } else {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs>0cf>91" ;                 }             });             setAttrs ( output );         });     }); });
1727191290

Edited 1727251836
GiGs
Pro
Sheet Author
API Scripter
It occurs to me after the fact, that if you can only have one equipped weapon at a time, you might store the dice roll outside the repeating section, to make it easier to access. The above worker automatically fills in the weapon roll for every weapon. You might want to install a default, like this: <input type="hidden" name="attr_psydice3" value="1d100cs>0cf>91"/> And have another input outside the repeating section like <input type="hidden" name="attr_psydice3_equipped" value="1d100cs>0cf>91"/> (Change its name to whatever is appropiate). Then the sheet worker would be adjusted, to something like this: on ( "change:repeating_psypowers3:epr3 change:psyrating sheet:opened" , function ( event ) {     const rowID = event . triggerName . split ( '_' )[ 2 ];     getSectionIDs ( 'repeating_psypowers3' , function ( ids ) {         const fields = [];         ids . forEach ( function ( id ) {             fields . push ( `repeating_psypowers3_ ${ id } _epr3` );         });         getAttrs ([... fields , "psyrating" ], function ( values ) {             var psyrating = parseInt ( values [ "psyrating" ]);             var output = {};             output . psydice3_equipped = "1d100cs>0cf>91" ;             ids . forEach ( function ( id ) {                 var epr = parseInt ( values [ `repeating_psypowers3_ ${ id } _epr3` ]);                 if ( psyrating > epr ) {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs101cf>91" ;                 } else if ( psyrating == epr ) {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99cs100cf>91" ;                 } else {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs>0cf>91" ;                 }                 if ( rowID == id ) {                     if ( psyrating > epr ) {                         output . psydice3_equipped = "1d100cs101cf>91" ;                     } else if ( psyrating == epr ) {                         output . psydice3_equipped = "1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99cs100cf>91" ;                     } else {                         output . psydice3_equipped = "1d100cs>0cf>91" ;                     }                 }             });             setAttrs ( output );         });     }); }); This means that you can always get the dice for whichever is equipped by using @{psydice3_equipped} - you don't have to mess about with repeating section syntax. If a weapon is equipped, it'll use the correct value, and if no weapon is equipped, it'll use the else value.
1727251902
GiGs
Pro
Sheet Author
API Scripter
I just fixed a seriously bad typo in that last sheet worker, in case you have tried it. It should work now.
1727328627

Edited 1727331085
Mago
Sheet Author
hello GiGs and thank you for your great input this is really confusing for me, im not very literate in JS, im pretty sure im missing something here the idea is for each repeating section to have its own hidden dice that changes when the <select> value changes i wanted this to work no matter how many repeating sections are added problem is the <select> attr is also inside the repeating section the whole repeating section syntax is very confusing i copied and pasted your last post code but the script does not seem to change the value, when i tried console.log it says that " Uncaught ReferenceError: on is not defined" (this is firefox) so i took your initial advice and expanded on it, i place the <select> and the dice outside , and went back to my script on("change:psyrating change:epr sheet:opened", function() {   getAttrs(["epr","psyrating"], function(values) {       var epr = parseInt(values["epr"]);       var psyrating = parseInt(values["psyrating"]);       var output = "";       if (psyrating > epr) {         output = "1d100cs101cf>91";       }       else if (psyrating == epr) {         output = "1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99cs100cf>91";       }       else {         output = "1d100cs>0cf>91";       }       setAttrs({         "psydice": output     });   }); }); now because reasons, its only giving me the else output :(
1727364990
GiGs
Pro
Sheet Author
API Scripter
I didn't test it, I wouldn't be surprised if my code contains a syntax error. I'll test it tonight. The basic procedure outlined is good, though. What HTML are you using for that repeating section now?
1727373323

Edited 1727374613
GiGs
Pro
Sheet Author
API Scripter
The main problem is this: < select name= "attr_epr3" >         < option disabled style= "background:gray;font-weight:bold;color:white;" > Psy Strength </ option >         < option value= "(ceil(@{psyrating}/2))" > Fettered </ option >         < option selected value= "@{psyrating}" > Unfettered </ option >         < option value= "(@{psyrating} + 3)" > Push </ option >     </ select > in combination with the if statement: if ( psyrating > epr ) { The problem is that the sheet worker doesnt read this @{psyrating} as a number, it reads it as tje literal text  " @{psyrating} " . So the psyrating > epr cannot generate a true/false result - it produces an undefined error. The way to fix this, is to change that select to generate an actual number. I'll show how to do that below. The general rule is sheet workers do not work with Roll20 attributes, when you want an actual number. You need to use another sheet worker to generate an actual number. There's a second problem, that only applies sometimes, where the select has no value until one is selected, and the displayed choice is not the actual value until you choose something else and then change back to that. This is just a problem with how selects work (at least on roll20), and we'll solve that at the same time.
1727379098
GiGs
Pro
Sheet Author
API Scripter
This method should work, but I'm being hit by a current roll20 bug that every 2nd change event in a repeating section is ignored. So if you see a glitch, try changing something again. Change the repeating section html as follows: < input name= "attr_psyrating" type= "number" value= "5" /> < fieldset class= "repeating_psypowers3" name= "attr_psypowers3" >     < span > fieldset </ span >< br >     < select name= "attr_epr3_text" >         < option selected disabled style= "background:gray;font-weight:bold;color:white;" > Psy Strength </ option >         < option > Fettered </ option >         < option > Unfettered </ option >         < option > Push </ option >     </ select >     < input type= "hidden" name= "attr_epr3" value= "" />     < input type= "hidden" name= "attr_psydice3" value= "" /> </ fieldset > Notice that the title option now has selected, so that select doesn't have the problem of showing the wrong item. I've also changed its name, and added a seprate epr attribute, which will include the numerical value. Now, use this sheet worker: on ( "change:repeating_psypowers3:epr3_text change:psyrating sheet:opened" , function () {     getSectionIDs ( 'repeating_psypowers3' , function ( ids ) {         const fields = ids . map ( id => `repeating_psypowers3_ ${ id } _epr3_text` );         getAttrs ([... fields , "psyrating" ], function ( values ) {             var psyrating = parseInt ( values [ "psyrating" ]);             var output = {};             ids . forEach ( function ( id ) {                   var epr_text = values [ `repeating_psypowers3_ ${ id } _epr3_text` ];                 var epr = 0 ;                 if ( epr_text == 'Fettered' ) {                     epr = Math . ceil ( psyrating / 2 );                                     } else if ( epr_text == 'Unfettered' ) {                     epr = psyrating ;                 } else if ( epr_text == 'Push' ) {                     epr = psyrating + 3 ;                 }                 output [ `repeating_psypowers3_ ${ id } _epr3` ] = epr ;                                 if ( psyrating > epr ) {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs101cf>91" ;                 } else if ( psyrating == epr ) {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs11cs22cs33cs44cs55cs66cs77cs88cs99cs100cf>91" ;                 } else {                     output [ `repeating_psypowers3_ ${ id } _psydice3` ] = "1d100cs>0cf>91" ;                 }             });             console . info ({ output });             setAttrs ( output );         });     }); }); Notice that several items respond to epr_text not epr now. The worker updates the hidden epr attribute, so none of your macros that already use it need to be changed.
1727383071
GiGs
Pro
Sheet Author
API Scripter
You mentioned planning to use this technique for multiple repeating sections. Is it always to produce the same kind of roll output and linked to the same kind of select? I ask because it is possible to ake the code repeatable for multiple selects, so it only needs to be written once.
1727581922
Mago
Sheet Author
oh GiGs you nail it, the <select> name="attr....." is not a number, this is a real headache, i will try your code  thank you
1727715927
GiGs
Pro
Sheet Author
API Scripter
The fact that we are talking about a reperating drction is worrying. That means it is hit hard by the current roll20 bug . If things that should work don't seem to be working, create another row and try on that.