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

Custom Character Sheet - Changing background color depending on an input value ?

Hello all ! I'm currently making a small custom sheet for my campaign and I got to the point where I would need some basic Javascript. I thought it would not be too much trouble to implement but I guess making it works in Roll20 is not as easy as planned unfortunately ... Basically I have several cells with skills levels in them and I would like their background color to change depending on the level of the related input value in the cell. So on a normal web page this would be the code I use : HTML : <div class="sheet-cell">Fencing<input type="number" name="attr_Fencing" value="0" min="0" max="3"></div> <div class="sheet-cell">Archery<input type="number" name="attr_Archery" value="0" min="0" max="3"></div> <div class="sheet-cell">Witchcraft<input type="number" name="attr_Witchcraft" value="0" min="0" max="3"></div> <!-- Other skills .... --> CSS : .sheet-cell.bg-color-0 { background-color: white;} .sheet-cell.bg-color-1 { background-color: lightyellow;} .sheet-cell.bg-color-2 { background-color: lightgreen;} .sheet-cell.bg-color-3 { background-color: green;} Script : <script>   function updateCellBackground(input) {     const cell = input.closest('.sheet-cell');     const value = parseInt(input.value);     cell.classList.remove('bg-color-0', 'bg-color-1', 'bg-color-2', 'bg-color-3');     cell.classList.add(`bg-color-${value}`); } const inputs = document.querySelectorAll('.cell input[type="number"]'); inputs.forEach(input => {     updateCellBackground(input);     input.addEventListener('input', () => updateCellBackground(input)); }); </script> Now, I've tried to adapt the script for Roll20 and this is what I came up with at the moment : <script type="text/worker"> function updateCellBackground(attributeName) { getAttrs([attributeName], function(values) { const value = parseInt(values[attributeName]) || 0; const cellClass = `bg-color-${value}`; const cellElement = document.querySelector(`[name="attr_${attributeName}"]`).closest('.sheet-cell'); cellElement.classList.remove('bg-color-0', 'bg-color-1', 'bg-color-2', 'bg-color-3'); cellElement.classList.add(cellClass); }); } // Skills to check const attributes = ['Fencing', 'Archery', 'Witchcraft']; // Other skills ... attributes.forEach(attr => { on(`change:${attr}`, function() { updateCellBackground(attr); }); }); on('sheet:opened', function() { attributes.forEach(attr => { updateCellBackground(attr); }); }); </script> And I didn't have much success since ... To be totally honest I'm not even sure if that's supposed to work and I'm kind of stuck ... Can this script work or am I making mistakes somewhere ? Is there another solution, maybe an easier one, to get the wanted result in this case ? If you have any advice or insight they would be very welcomed ! Thanks in advance all :) !
1728163682

Edited 1728193740
GiGs
Pro
Sheet Author
API Scripter
Roll20 javascript is unlike any you'll find on other sites. I strongly recommend checking out the wiki and my site . Many of normal JS code snippets you will find on other sides simply will not work on roll20. As a guide, anything with document. in it will not work. For changing the appearance of a page, you must use CSS methods, and these are often combined with roll20's hidden atttributes. For your HTML, you'll need something like <input type="hidden" name="attr_Fencing" value="0" class="sheet-toggle"> <div class="sheet-cell">Fencing<input type="number" name="attr_Fencing" value="0" min="0" max="3"></div> <input type="hidden" name="attr_ Archery " value="0" class="sheet-toggle"> <div class="sheet-cell">Archery<input type="number" name="attr_Archery" value="0" min="0" max="3"></div> <input type="hidden" name="attr_ Witchcraft " value="0" class="sheet-toggle"> <div class="sheet-cell">Witchcraft<input type="number" name="attr_Witchcraft" value="0" min="0" max="3"></div> <!-- Other skills .... --> Notice you need a hidden attribute directly before each div with a CSS class. Then in the CSS, you need one declaration that will suffice for every skill. Well, one per level. .sheet-toggle[value="0"] + .sheet-cell { background-color: white;} .sheet-toggle[value="1"] + .sheet-cell { background-color: lightyellow;} .sheet-toggle[value="2"] + .sheet-cell { background-color: lightgreen;} .sheet-toggle[value="3"] + .sheet-cell { background-color: green;} You will need to make sure that those numbers are exact integers., though that probably means just telling the players that. Note this part of the input code does nothing on roll20: min="0" max="3". If you really want to constrain those values,you will need to use javascript.
1728163974
GiGs
Pro
Sheet Author
API Scripter
The code follwoing will not work on roll20, but I can see what you are going for and if it did work , I'll show ou another qway to do it. The non-working code: attributes.forEach(attr => { on(`change:${attr}`, function() { updateCellBackground(attr); }); }); on('sheet:opened', function() { attributes.forEach(attr => { updateCellBackground(attr); }); }); The streamlined code: attributes.forEach(attr => { on(`change:${attr.toLowerCase()} sheet:opened`, function() { updateCellBackground(attr); }); }); If you really wanted, you could change updateCellBackground(attr) to a function that constrains the value to an integer and no lower than 0 and no hhigher than 3.
Ah this actually works quite well with just the hidden input and CSS code indeed ! Thanks a lot for this simple workaround ! I think I will not bother with the JS script then... Keeping the integer between 0 and 3 is not a big deal as you said, just have to tell the players. Also, Roll20 does actually block the value when you use the arrow sof the number type input so all good on this part. Thanks again !
1728191169

Edited 1728191332
GiGs
Pro
Sheet Author
API Scripter
Le Chroniqueur [Vincent] said: Also, Roll20 does actually block the value when you use the arrow sof the number type input so all good on this part. Thanks again ! You're welcome! Also, shockingly, I wasn't aware of min and max working (if you use the arrows). I think it didn't used to work (maybe it only works on modern sheets and not Legacy sheets - I'll have to test). You can manually enter any number though. With such a small range, players are more likely to use the arrows, so that JS is even less necessary. PS: if you do start to use javascript, or need any other help on the sheet, my site might be helpful: <a href="https://cybersphere.me/roll20/" rel="nofollow">https://cybersphere.me/roll20/</a>