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

Simple Javascript not working ?

1680812070
Shey L'Ours
Pro
Sheet Author
Hello there, I'm pretty sure i've miss something about Roll20 compatibility, but what ? This JS is working as expected on CodePen for example (<a href="https://codepen.io/spirito35/pen/ExdxpwE" rel="nofollow">https://codepen.io/spirito35/pen/ExdxpwE</a>) but doesn't work when it goes to character sheet :X Thans for your help :( &lt; div id = "sheet-stats1" &gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt; input type = "number" value = "0" name = "attr_ref-calculated" id = "ref" onchange = " updateAcValue ()" &gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt; input type = "number" value = "0" name = "attr_nat-calculated" id = "nat" onchange = " updateAcValue ()" &gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt; input class = "sheet-statistics1" type = "number" value = "0" name = "attr_ac-calculated" id = "ac" &gt; &nbsp;&lt;/ div &gt; &lt; script type = "text/worker" &gt; &nbsp; &nbsp; &nbsp; &nbsp; function updateAcValue() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var natValue = document.getElementById('nat').value; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var refValue = document.getElementById('ref').value; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('ac').value = Math.max(natValue, refValue); &nbsp; &nbsp; &nbsp; &nbsp; } &lt; script &gt;
1680813173
The Aaron
Roll20 Production Team
API Scripter
Character sheets don't work like normal websites.&nbsp; You don't have access to document, and you don't change values by getting elements.&nbsp; Things you want to change must be represented as attributes, and you manipulate those attributes to change what is displayed.&nbsp; You react to change events on those attributes to make calculations or enforce bounds or the like.&nbsp; You can read more about it in here:&nbsp;<a href="https://help.roll20.net/hc/en-us/articles/360037773513#getattrs-attributenamearray-callback-asynchronous-0-15" rel="nofollow">https://help.roll20.net/hc/en-us/articles/360037773513#getattrs-attributenamearray-callback-asynchronous-0-15</a>
1680815529
Shey L'Ours
Pro
Sheet Author
That's fast, thanks. Here it is, so I must change for something like... that (if i'm learning good). But it is not fixed yet... :X Time to learn faster I think ! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;on("change:phy-calculated change:nat-calculated", function() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getAttrs(["phy-calculated", "nat-calculated"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var phy = parseInt(values["phy-calculated"]) || 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var nat = parseInt(values["nat-calculated"]) || 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ac-calculated: Math.max(nat, phy) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
1680816955
The Aaron
Roll20 Production Team
API Scripter
I think I'd steer clear of `-` in attribute names.&nbsp; For one thing, you can't use them directly in property names for objects in Javascript, so you'd have to write the above like this: setAttrs({ "ac-calculated": Math.max(nat, phy) }); They also take on some special meanings in repeating section attribute names, if I remember correctly.
1680818667
Shey L'Ours
Pro
Sheet Author
You were perfectly right, I've delete all the "-" and it work perfectly. Thanks a lot mate ! on("change:phycalc change:natcalc", function() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getAttrs(["phycalc", "natcalc"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var phy = parseInt(values["phycalc"]) || 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var nat = parseInt(values["natcalc"]) || 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var maxVal = Math.max(phy, nat); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accalc: maxVal &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
1680877126
GiGs
Pro
Sheet Author
API Scripter
By the way, an underscore is valid in place of the hyphen. If you look up javascript variable names, you'll find what's allowed.