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

Need to assign letters to text boxes, rank values, and put letters in text fields.

New Here ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

Hey Peeps, 

 

I have come to this wonderful community many times for answers with great success. I have another one for you. 

 

I'm working on an assessment PDF with 6 categories. Each of these categories have it own letters that the assessment selections can add up to when people click a checkbox. On the left and right column the letters each add up and populate at the bottom of the columns and then a line below that, there is the grand total for each corresponding letter. These grand totals also populate in text fields Total, Total_2, Total_3, Total_4, Total_5, and Total_6 I'm attaching the document for you to all see.

 

What I need to do is:
1) Have each of the corresponding text fields (Total, Total_2, Total_3, Total_4, Total_5, Total_6) have their own letter values (in this case each corresponding text box would go as follows: RIASEC).

2) Then I need to be able to have the top 3 highest values from the 6 text fields (Total, Total_2, Total_3, Total_4, Total_5, Total_6) to then be determined.

3) From those 3 totals, the corresponding letters (RIASEC) for the highest values will then auto-fill into the bottom 3 boxes with the top ranking letter going into the text field "MY INTEREST CODE", the second ranking in text field "undefined", and the third ranking in text field "undefined_2". 

 

Thank you so much in advance!

TOPICS
How to , JavaScript , PDF , PDF forms

Views

728

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 ,
Jul 02, 2023 Jul 02, 2023

Copy link to clipboard

Copied

It's easier to do such things when the fields are named consistently. I renamed the first total field as "Total_1" and the last two "interest code" fields as "MY INTEREST CODE_2" and "MY INTEREST CODE_3" and then used the following code as the custom calculation script for "MY INTEREST CODE_1":

 

 

var letters = "RIASEC";
var totals = [];
for (var i=1; i<=6; i++) {
	totals.push({letter: letters.charAt(i-1), total: Number(this.getField("Total_"+i).valueAsString)});
}
totals.sort(sortTotals);
event.value = totals[0].letter;
this.getField("MY INTEREST CODE_2").value = totals[1].letter;
this.getField("MY INTEREST CODE_3").value = totals[2].letter;

function sortTotals(a,b) {
	return b.total-a.total;
}

 

 

The edited file is also attached.

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 ,
Jul 02, 2023 Jul 02, 2023

Copy link to clipboard

Copied

It's easier to do such things when the fields are named consistently. I renamed the first total field as "Total_1" and the last two "interest code" fields as "MY INTEREST CODE_2" and "MY INTEREST CODE_3" and then used the following code as the custom calculation script for "MY INTEREST CODE_1":

 

 

var letters = "RIASEC";
var totals = [];
for (var i=1; i<=6; i++) {
	totals.push({letter: letters.charAt(i-1), total: Number(this.getField("Total_"+i).valueAsString)});
}
totals.sort(sortTotals);
event.value = totals[0].letter;
this.getField("MY INTEREST CODE_2").value = totals[1].letter;
this.getField("MY INTEREST CODE_3").value = totals[2].letter;

function sortTotals(a,b) {
	return b.total-a.total;
}

 

 

The edited file is also attached.

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 ,
Jul 03, 2023 Jul 03, 2023

Copy link to clipboard

Copied

Thank you so much for this! And I'm going to fan girl for a moment. Try67, you've helped me with so much code for the documents I've created. I appreciate you as a teacher and Community Expert. 

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 ,
Jan 30, 2025 Jan 30, 2025

Copy link to clipboard

Copied

LATEST

 

Hi @Alyssa5FBC ,

You can achieve this with JavaScript in your PDF form. Assign each total field (Total, Total_2, etc.) its corresponding letter (RIASEC). Then, sort the values in descending order and populate the top three into "MY INTEREST CODE," "undefined," and "undefined_2." Use JavaScript like below and learn from this site:

 

javascript
var scores = [ { letter: "R", value: this.getField("Total").value }, { letter: "I", value: this.getField("Total_2").value }, { letter: "A", value: this.getField("Total_3").value }, { letter: "S", value: this.getField("Total_4").value }, { letter: "E", value: this.getField("Total_5").value }, { letter: "C", value: this.getField("Total_6").value } ]; scores.sort((a, b) => b.value - a.value); this.getField("MY INTEREST CODE").value = scores[0].letter; this.getField("undefined").value = scores[1].letter; this.getField("undefined_2").value = scores[2].letter;

This should work—😊

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