Skip to main content
Participating Frequently
June 12, 2024
Question

Add color to JavaScript string variables.

  • June 12, 2024
  • 1 reply
  • 562 views

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! 

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
June 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;

 

Participating Frequently
June 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? 

 

try67
Community Expert
Community Expert
June 13, 2024

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

Check the JS Console for errors.