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

How to get checked box to modify an attribute

April 11 (5 years ago)

I'm building a character sheet to test a very early version of an RPG I'm designing—something fun to play with friends. I figure this would be a good opportunity to familiarize myself with building sheets in Roll20 and javascript. Hence my probably very simple problem:

The parts: There are three fields to each skill [Career(checkbox), Aptitude Score(input:number), and Base Score]. In this case, the skill is Anthropology (shortened to 'anthro' in the code).

My goal: The Base Score is supposed to change depending on whether the Career checkbox is checked. If it is, the Base Score is the Aptitude Score * 10. Otherwise, it is 0. My problem is that nothing is calculated or appears on the character sheet in the game when the box is checked.

The code:

<script type="text/worker">
    on("sheet:opened","change:anthrocareer", function() {
        getAttrs(["anthroCareer","anthroApt"], function(values) {
            let checked = parseInt(values.anthroCareer)||0;
            let aptitude = parseInt(values.anthroApt,10)||0;
            let basescore = 0;
            if(checked === 1) {
                basescore = aptitude * 10;
            }
            
            setAttrs({
                anthroBase:basescore
            });
        });
    });
</script>
April 11 (5 years ago)

Edited April 11 (5 years ago)
GiGs
Pro
Sheet Author
API Scripter

The problem is likely the on line, which should be

on("sheet:opened change:anthrocareer", function() {

If you have multiple events there, they all go inside the same string: just one set of quotes and no comma between them.


April 11 (5 years ago)

Thanks for catching that!

After some poking and prodding, I got the code to do just about 75% of what I need it to do. Looking at other posts has helped me clean up some of my more egregious errors.

Here's the code with the HTML as well. The issue I keep encountering is that the checkbox, when checked, does not calculate the base score (aptitude * 10). However, when I check or uncheck the box, the Base Score gets reset to 0.

<div class="grid-container attrs-container" name="attributes-and-skills">
<!-- HEADER -->
    <div class="grid-item">
        <!-- BLANK SPACE -->
    </div>
    <div class="grid-tem">
        <p>Career</p>
    </div>
    <div class="grid-item">
        <p>Aptitude</p>
    </div>
    <div class="grid-item">
        <p>Base Score</p>
    </div>
    <div class="grid-item">
        <p>Roll</p>
    </div>
    <div class="grid-item">
        <p>Total Score</p>
    </div>

<!-- ANTHROPOLOGY -->
    <div class="grid-item-skill">
        <p>Anthropology</p>
    </div>
    <div class="grid-tem">
        <input class="checkbox" type="checkbox" name="attr_anthro_career">
    </div>
    <div class="grid-item">
        <input class="input-field" type="number" name="attr_anthro_apt">
    </div>
    <div class="grid-item">
        <input class="input-field" type="number" name="attr_anthro_base">
    </div>
    <div class="grid-item">
        <input class="input-field" type="number" name="attr_anthro_roll">
    </div>
    <div class="grid-item">
        <input class="input-field" type="number" name="attr_anthro_score" readonly>
    </div>

<script type="text/worker">
    on("change:anthro_career change:anthro_apt", function() {
        getAttrs(["anthro_career","anthro_apt"], function(values) {
            let checked = parseInt(values.anthro_career)||0;
            let aptitude = parseInt(values.anthro_apt,10)||0;
            let basescore = 0;
            if (checked === 1) {
                basescore = aptitude * 10;
            }
            setAttrs({
                anthro_base:basescore
            });
        });
    });
</script>
April 12 (5 years ago)
GiGs
Pro
Sheet Author
API Scripter

You need to include value="1" in the checkbox. By default, a checked checkbox has a value of "on".

<input class="checkbox" type="checkbox" name="attr_anthro_career" value="1">
April 12 (5 years ago)
GiGs
Pro
Sheet Author
API Scripter

Do note though, that this will always reset the base score, so if you are planning to have people manually modify it, its not a good approach, because if they check or uncheck the checkbox, any changes they have made will be overwritten.