Skip to main content
Participant
May 7, 2018
Answered

Auto populate a field based on a range in another field

  • May 7, 2018
  • 1 reply
  • 567 views

I'm creating a character sheet for an rpg and I'd like to have it auto-calculate the values for attributes, modifiers, and skills:

  • The Attribute field will be a sum of two numbers, the person's originally rolled number for that attribute and any racial modifier.
  • Based on the resulting sum of those two numbers, the Modifier field will fill itself with a predetermined number (e.g. If your Strength ends up being 14, your Modifier will be +2).
  • Then, the number in the Modifier field will be used as a base to calculate another sum of numbers, this time for Skills.

I've been able to come up with how to do the sums easily enough, but how to populate a field with a specific number based on a range eludes me.

I am pretty terrible at JavaScript when it comes to PDFs, but I feel like this should be relatively simple yet I've had no success. Any suggestions?

EDIT: Here's what I've got so far

var brTotal = Number(this.getField("BrTotal").value); 

   

if (brTotal<=1) event.value = -3; 

else if (brTotal=2) event.value = -2; 

else if (brTotal=3) event.value = -1;

else if (brTotal>=4 && brTotal<=7) event.value = 0; 

else if (brTotal>=8 && brTotal<=9) event.value = 1;

It works... Kinda. When I enter brTotal as 1 or 2, I get the right values, but if I enter 3 or above, it's stuck at -2. Am I missing something?

This topic has been closed for replies.
Correct answer try67

You have an error in your code. The comparison operator of JS is "==", not "=". That's the value assignment operator.

So change these two lines:

else if (brTotal=2) event.value = -2;   

else if (brTotal=3) event.value = -1; 

To:

else if (brTotal==2) event.value = -2;   

else if (brTotal==3) event.value = -1; 

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
May 7, 2018

You have an error in your code. The comparison operator of JS is "==", not "=". That's the value assignment operator.

So change these two lines:

else if (brTotal=2) event.value = -2;   

else if (brTotal=3) event.value = -1; 

To:

else if (brTotal==2) event.value = -2;   

else if (brTotal==3) event.value = -1;