Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Need help in a form to calculate the value of a field based on the visibility of radio buttons

Community Beginner ,
Jan 01, 2025 Jan 01, 2025

I'm creating a sheet for a game.  I have a list of skills with 3 radio buttons stacked on top of each other next to each skill.  The idea is that if you do not have that skill, the "Blank" radio button is visible.  If you have partial skill in it, the "Partial" button (which is a half circle image) is visible, and if you are fully skilled, the "Skilled" button is visible (full circle image).  When one is visible, the other two are hidden. I am using Run Javascript on Mouse Up as the action for each button to hide/show each one.  That script is very simple and looks like this:


this.getField("PartialSkillLift").display = display.visible;

this.getField("SkilledLift").display = display.hidden ;

this.getField("BlankLift").display = display.hidden ;

 

That part is working fine, but what i would like to add to it that i can't seem to get figured out is i would like the skill to show a number or do a calculation based on which button is showing.  So, if the Blank button is visible, it just displays the number from a stat bonus (i.e. "StrengthBonus" field). If the Partial button is shown, it does the calculation of "StrengthBonus" + 1).  And if the Skilled button is shown, it does the calculation "StrengthBonus + Level + 3" ("Level" is also a field already set up).

 

I have this in the "Lift" field currently...

 

strbonus = this.getField("StrengthBonus").value;
level = this.getField("Level").value;
skilllift = this.getField("SkilledLift").display = display.visible;
blanklift = this.getField("BlankLift").display = display.visible;
partlift = this.getField("PartialSkillLift").display = display.visible;
 
 
if (strbonus !=="" && skilllift !=="" && level !=="")
{
this.getField("Lift").value = strbonus + level + 3;
}
else if (strbonus !=="" && blanklift !=="")
{
this.getField("Lift").value = strbonus;
}
else if (strbonus !=="" && partlift !=="")
{
this.getField("Lift").value = strbonus + 1;
}
else event.value = "";
 
Which, when the strbonus value is entered, that number populates correctly in the "Lift" field, and once you add a value to the "Level" field, it correctly does the math (strbonus + level +3) and displays it in the "Level" field....
A couple of issues though are that all of the button fields are just always visible (which is not intended), and clicking throught the buttons doesn't actually have any effect on the values at all.  The math that is working doesn't seem to be tied to the button presses at all...it's just doing the math options for when the two fields that feed it are entered. 
 
I'm really new to javascript, so i've pretty much just been trying to teach myself this as i go through google searches and videos.  I've had a lot of success at the rest of this form, but this last piece really has me stumped.  Any assistance i can get would be greatly appreciated.

 

TOPICS
How to , JavaScript , PDF , PDF forms
709
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 01, 2025 Jan 01, 2025

"all of the button fields are just always visible "  That's because these lines of code are setting them to visible:

skilllift = this.getField("SkilledLift").display = display.visible;
blanklift = this.getField("BlankLift").display = display.visible;
partlift = this.getField("PartialSkillLift").display = display.visible;
Also, the statements
skilllift !=="" 
blanklift !==""
partlift !==""
will always return true because you are setting their values to 0 with the previous 3 lines I listed.
Tip:  You don't need 3 buttons with show/hide to get your 3 images.  You can use one button that changes the icon whenever it is clicked.  Read this article:
Then store a value for each icon displayed in the button that changes when the button is clicked and use that value in your calculations.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 02, 2025 Jan 02, 2025

Thank you so much for the assistance with the carousel button.  That is SUPER nice, to only have one button for each skill instead of three for each.  Way cleaner.  I haven't had a chance to have the new buttons values start feeding the Skill yet, but thats my next task.  Was just really excited to find out that the buttons could carousel in that way though.  Very helpful, and i appreciate the reply!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2025 Jan 03, 2025

You can use the same stored icons for all your buttons that will have the same click through images.  Button fields don't have a text property but you can store a value for the different icons being display in a hidden text field, or in the tooltip.  Example:

event.target.userName=0;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 03, 2025 Jan 03, 2025

So, i have gone into the field that the carousel button was supposed to feed values into and entered some script.  I'm not sure what i'm doing incorrectly.  When the "strbonus" field has a number entered into it, the script is populating into the "Lift" field the way it should (especially since the buttons default state is "Blankdot")...but when i cycle through the other button presses...it's not changing the values the way it should.  I'm obviously missing something in my script that is needed, i just don't know what it is.  Below is the script i have entered into the "Lift" field:

 

strbonus = this.getField("StrengthBonus").value;
level = this.getField("Level").value;
 
 
if (event.target.userName="Blankdot")
{
this.getField("Lift").value = strbonus;
}else
if (event.target.userName="Partdot")
{
this.getField("Lift").value = strbonus + 1;
}else
if (event.target.userName="Skilldot")
{
this.getField("Lift").value = strbonus + level + 3;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2025 Jan 03, 2025

You need to use two equal signs for comparison in the 'if' and 'else' statements:

  1. if (event.target.userName == "Blankdot")

 

event.target will work in the field where the script is, if you want to get tooltip from other fields then you can do it like this:
if(this.getField("Lift").userName == "Pardot")

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2025 Jan 04, 2025

As @Nesa Nurani said, you need 2 equal signs for each comparison.  You also need to name the button field instead of event.target, which is the Lift field in this case.

if(this.getField("ButtonFieldName").userName=="Blankdot")

Replace "ButtonFieldName" with the actual name of the field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 04, 2025 Jan 04, 2025

I greatly appreciate all the help and patience. A lot of this is very new for me, so i'm really trying to learn/understand best i can.  I actually wondered if the "ButtonFieldName" should have been called out somewhere, but wasn't sure exactly how it should go, so when i read your response this morning i was excited that that would solve it.  

I have altered the code according to the recommendations from you both, and it now looks as follows:

 

strbonus = this.getField("StrengthBonus").value;
level = this.getField("Level").value;
 
if(this.getField("Button7").userName=="Blankdot")
{
this.getField("Lift").value = strbonus;
}else
if(this.getField("Button7").userName=="Partdot")
{
this.getField("Lift").value = strbonus + 1;
}else
if(this.getField("Button7").userName=="Skillkdot")
{
this.getField("Lift").value = strbonus + level + 3;
}

 

However...it still isn't doing the calculations for when the button is clicked to "Partdot" or "Skilldot".  When a value is entered into "strbonus", it shows in "Lift" because the "Blankdot" is shown, but it doesn't change as i cycle through the button changes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2025 Jan 04, 2025

Changing tooltip by clicking the button will not trigger calculation script, instead you have a couple of choices, either create another calculation script and place it in the button as Mouse UP or create document level script inside a function then call function from your calculated field and from the button.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2025 Jan 04, 2025

Add this line at the end of your button script:

this.calculateNow();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 04, 2025 Jan 04, 2025
LATEST

Oh my goodness, thank you SO MUCH!!   It is now working exactly the way i need it to!!  I GREATLY appreciate your assistance!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines