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

Calculate a field based on radio button selection

New Here ,
Mar 19, 2020 Mar 19, 2020

Copy link to clipboard

Copied

Hi all,

 

I have a form field (“RB_Inc_9”) that I want to auto-calculate based on what radio button is selected. Please see the below screenshot for reference:

 

form.jpg

Here’s what I want to see:

  • If the “Salary” radio button is selected, I want the following calculation to run:
    • event.value = this.getField("OH").value * this.getField("RB_Inc_1").value + this.getField("OH").value * this.getField("RB_Inc_2").value
  • If the “Direct Costs” radio button is selected, I want the following calculation to run:
    • event.value = this.getField("OH").value * this.getField("RB_Inc_1").value + this.getField("OH").value * this.getField("RB_Inc_2").value + this.getField("OH").value * this.getField("RB_Inc_3").value + this.getField("OH").value * this.getField("RB_Inc_4").value + this.getField("OH").value * this.getField("RB_Inc_5").value + this.getField("OH").value * this.getField("RB_Inc_6").value + this.getField("OH").value * this.getField("RB_Inc_7").value + this.getField("OH").value * this.getField("RB_Inc_8").value
  • If the “Other” radio button is selected, I want no javascript calculation to run

 

I’ve done some research but still can’t figure out exactly how to run if/then statements in Javascipt.

 

Thanks!

TOPICS
How to , PDF forms

Views

3.5K

Translate

Translate

Report

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
1 ACCEPTED SOLUTION
Community Expert ,
Mar 19, 2020 Mar 19, 2020

Copy link to clipboard

Copied

Use this:

 

var overhead = this.getField("RB_Inc_9").valueAsString;
if (overhead=="Salary") {
	// insert code here
} else if (overhead=="Direct Costs") {
	// insert code here
}

View solution in original post

Votes

Translate

Translate

Report

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 ,
Mar 19, 2020 Mar 19, 2020

Copy link to clipboard

Copied

Use this:

 

var overhead = this.getField("RB_Inc_9").valueAsString;
if (overhead=="Salary") {
	// insert code here
} else if (overhead=="Direct Costs") {
	// insert code here
}

Votes

Translate

Translate

Report

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
New Here ,
Mar 20, 2020 Mar 20, 2020

Copy link to clipboard

Copied

Hi, thanks very much for the input! I tried the code but it didn't work - see below (the IDs of the radio buttons are accurate according to my file; e.g. the Salary radio button = "OH_Sal", and the Direct Cost radio button = "OH_DC"): Let me know if you have any suggestions!

 

var OH_Calc = this.getField("RB_Inc_9").valueAsString;
if (OH_Calc=="OH_Sal") {
// event.value = this.getField("OH").value * this.getField("RB_Inc_1").value + this.getField("OH").value * this.getField("RB_Inc_2").value
} else if (OH_Calc=="OH_DC") {
// event.value = this.getField("OH").value * this.getField("RB_Inc_1").value + this.getField("OH").value * this.getField("RB_Inc_2").value + this.getField("OH").value * this.getField("RB_Inc_3").value + this.getField("OH").value * this.getField("RB_Inc_4").value + this.getField("OH").value * this.getField("RB_Inc_5").value + this.getField("OH").value * this.getField("RB_Inc_6").value + this.getField("OH").value * this.getField("RB_Inc_7").value + this.getField("OH").value * this.getField("RB_Inc_8").value
}

Votes

Translate

Translate

Report

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 ,
Mar 20, 2020 Mar 20, 2020

Copy link to clipboard

Copied

Remove the "//" part... That's how you create a comment.

Votes

Translate

Translate

Report

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
New Here ,
Mar 20, 2020 Mar 20, 2020

Copy link to clipboard

Copied

Still nothing! Not sure what I'm doing wrong...

 

var OH_Calc = this.getField("RB_Inc_9").valueAsString;
if (OH_Calc=="OH_Sal") {
    event.value = this.getField("OH").value * this.getField("RB_Inc_1").value + this.getField("OH").value * this.getField("RB_Inc_2").value
} else if (OH_Calc=="OH_DC") {
    event.value = this.getField("OH").value * this.getField("RB_Inc_1").value + this.getField("OH").value * this.getField("RB_Inc_2").value + this.getField("OH").value * this.getField("RB_Inc_3").value + this.getField("OH").value * this.getField("RB_Inc_4").value + this.getField("OH").value * this.getField("RB_Inc_5").value + this.getField("OH").value * this.getField("RB_Inc_6").value + this.getField("OH").value * this.getField("RB_Inc_7").value + this.getField("OH").value * this.getField("RB_Inc_8").value
}

 

Votes

Translate

Translate

Report

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 ,
Mar 20, 2020 Mar 20, 2020

Copy link to clipboard

Copied

Check the JS Console (Ctrl+J) for error messages.

Votes

Translate

Translate

Report

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
New Here ,
Dec 20, 2020 Dec 20, 2020

Copy link to clipboard

Copied

Good morning

I have a group of 3 radio buttons, I would need to select one of them to return a result in a text box.

The group is called group 1 and each button 1.1, 1.2, 1.3

Can you help me.

Votes

Translate

Translate

Report

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 ,
Dec 20, 2020 Dec 20, 2020

Copy link to clipboard

Copied

You can use this code as the custom calculation script of your text field:

 

var v = this.getField("group 1").valueAsString;

event.value = (v=="Off") ? "" : v;

Votes

Translate

Translate

Report

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
New Here ,
Dec 21, 2020 Dec 21, 2020

Copy link to clipboard

Copied

LATEST

Thanks a lot

Votes

Translate

Translate

Report

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