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

Add color to JavaScript string variables.

New Here ,
Jun 12, 2024 Jun 12, 2024

Hello,

 

I am attempting to add color to my variables that are inserted into standard text.

 

All varaiables can be colored "red" or rgb 255,0,0 or hex FF0000.

 

Here is my current code, 

 

var frequency = this.getField("Dropdown8").valueAsString;
var date = this.getField("Date7_af_date").valueAsString;
var target = this.getField("Text9").valueAsString;
var calcmonths = this.getField("Text99").valueAsString;
var periodend = this.getField("Dropdown88").valueAsString;
var dropdown3Value = this.getField("Dropdown3").valueAsString;

if (dropdown3Value === "Consolidate Debt Service Coverage Ratio C&I, CRE, AgCash, Non-Profit") {
 event.value = "This is test paragraph 1 " + target + " and " + frequency + " and " + calcmonths + " and " + date + " and " + calcmonths + " end paragraph.";} 
else if (dropdown3Value === "Consolidate Debt Service Coverage Ratio, Less Distributions C&I, CRE, AgCash") {
 event.value = "This is a test of paragraph 2 " + frequency + " and " + date + " and " + target + ".";} 
else if (dropdown3Value === "Consolidate Minimum Current Ratio C&I, CRE") {
 event.value = "This a test paragraph 3 " + target + " and " + periodend + " and " + date + ".";} 
else if (dropdown3Value === "Consolidate Maximum Debt/TNW C&I, CRE") {
 event.value = "This is a test paragraph 4 " + target + " and " + periodend + " and " + calcmonths + " end.";}
else
 event.value = "";

 

Thank you! 

TOPICS
JavaScript , PDF , PDF forms
609
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 ,
Jun 12, 2024 Jun 12, 2024

To do that you must define the field as having Rich Text Formatting, and then you'll need to specify the (rich) value as an array of Span objects, which can each have a different text color.

 

For example, this line:

event.value = "This is test paragraph 1 " + target + " and " + frequency + " and " + calcmonths + " and " + date + " and " + calcmonths + " end paragraph.";

 

Will become:

 

var spans = [];
spans.push({text: "This is test paragraph 1 ", textColor: color.black});
spans.push({text: target, textColor: color.red});
spans.push({text: " and ", textColor: color.black});
spans.push({text: frequency, textColor: color.red});
spans.push({text: " and ", textColor: color.black});
spans.push({text: calcmonths, textColor: color.red});
spans.push({text: " and ", textColor: color.black});
spans.push({text: date, textColor: color.red});
spans.push({text: " and ", textColor: color.black});
spans.push({text: calcmonths, textColor: color.red});
spans.push({text: " end paragraph.", textColor: color.black});
event.richValue = spans;

 

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
New Here ,
Jun 13, 2024 Jun 13, 2024

Thank you for the response, 

 

Once I apply this to my text box, (text5>properties>custom cal), I do not get any output.

 

Is that the correct setup? 

 

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 ,
Jun 13, 2024 Jun 13, 2024

Did you also add the first lines, which define the variables?

Check the JS Console for errors.

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 ,
Jun 13, 2024 Jun 13, 2024
LATEST

You need to include it into your script, and replace your line like this:

if (dropdown3Value === "Consolidate Debt Service Coverage Ratio C&I, CRE, AgCash, Non-Profit") {
event.value = "This is test paragraph 1 " + target + " and " + frequency + " and " + calcmonths + " and " + date + " and " + calcmonths + " end paragraph.";}

 

with:

if (dropdown3Value === "Consolidate Debt Service Coverage Ratio C&I, CRE, AgCash, Non-Profit") {
event.richValue = spans;}

 

For other conditions you need to create another span object, for example 'spans2'.

 

 

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