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

Javascript getAttrs help

My javascript is weak and i am not sure if this is even possible. I have a drop down menu that i would like to get the Attr value from. These are snippets that i hope will explain what i am trying to do. getAttrs(["npc_check","sr","ss","ss_dice","sr_dice","alchemy_dice" ,"animal_handling_dice" ,...."], function(v) { //get the values for the two attributes, so they can used.             var srval = String(v.sr.toLowerCase());             srval = srval+"_dice";                var srvaldice = String(v.srval.valueOf());     <-- Problem statement sr is basically a list of possible skills in a pull down menu (alchemy, animal_handling, etc) srval starts off being "alchemy" then gets concatenated and becomes "alchemy_dice" The value stored in the attr alchemy_dice is 1d6 as an example. I would like to make the variable srvaldice be equal to 1d6 in the above example. I have tried multiple variations to the problem statement with no luck. Any help would be appreciated. Thanks
1628049765
GiGs
Pro
Sheet Author
API Scripter
Firstly, you dont have to use String() with getAttrs. All attributes are automatically stored as strings, and if you need a different data type, you need to convert it - but if you need strings, you don't need to do anything. They are already strings. It would be easier to answer this if we could see the HTML for the relevant attributes, so we know for sure what's in them. But I think this should do what you need:      getAttrs ([ "npc_check" , "sr" , "ss" , "ss_dice" , "sr_dice" , "alchemy_dice"  , "animal_handling_dice"  ,],  function ( v ) {          var   srval  =  v . sr . toLowerCase ();          var   srvaldice  =  v [ srval + "_dice" ];      To understand what's happening here, getAttrs takes the array of attribute names, extracts their value for the sheet, and stores them in an javascript object that looks like this: v = {npc_check: 1, sr: 0, alchemy_dice: "2d6"} (and so on) This is a standard javascript data object, which you can google about to learn more how to manipulate it. There are two syntaxes you use to address the values within it. v.alchemy_dice v['alchemy_dice'] Either of these work, but the first follows javascript variable rules for naming, while the second accepts a string. So when you want to build an attribute name, and use that to get the value, you have to use the second format, where this kind of thing is valid:   srval = srval+"_dice";       var srvaldice = v[srval];     In my above code, I combined those into a single line for compactness, but you could keep them as separate lines like this.
Thanks, that fixed my problem. Thanks for explaining it well, I will have to review the JavaScript object like you suggested.