Skip to main content
Known Participant
November 15, 2020
Answered

Need help with javascript

  • November 15, 2020
  • 4 replies
  • 672 views

Can someone help me make these codes into one code?
I have ten fields named t1-t10,
in each field as custom calculation script I have these codes
in field1:
event.target.fillColor = this.getField("Text1").value >= 1 ? color.green : color.white;
in field2:
event.target.fillColor = this.getField("Text1").value >= 2 ? color.green : color.white;
in field3:
event.target.fillColor = this.getField("Text1").value >= 3 ? color.green : color.white;
...etc up to field 10:
event.target.fillColor = this.getField("Text1").value == 10 ? color.green : color.white;
I want to put them in one field and have as one code, I know how to make them like this:

event.target.fillColor = this.getField("Text1").value >= 1 ? color.green : color.white;
this.getField("t2").fillColor = this.getField("Text1").value >= 2 ? color.green : color.white;
...etc but I was wondering can it be done as one code something like:
var color = "";
for (var i=1; i<=10; i++) {
if (this.getField("t1"+i).value=="1") color++;
}
event.target.fillColor ? color.green : color.white;

Of course that doesn't work but you get idea what I'm looking for.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Nesa Nurani

If you want to use it as validation script add this to try67 code:


if (event.value < 0 || event.value > 10) {
app.alert("Your text goes here.");
event.rc = false;
}

4 replies

try67
Community Expert
Community Expert
November 15, 2020

Use this code as the custom Validation script of "Text1":

 

for (var i=1; i<=10; i++) {
	var f = this.getField("t"+i);
	f.fillColor = (Number(event.value)>=i) ? color.green : color.white;
}
MarietaaAuthor
Known Participant
November 15, 2020

Thank you, you'r always so helpful, and code works nice but I have new problem now, in that field I was using field value is in range 0-10 but if I use code I can't use that option anymore. Can I add something to the code to make it range 0-10 again?

try67
Community Expert
Community Expert
November 15, 2020

You can move that code to be a Calculation script, and then you'll still be able to use the built-in range Validation option.

 

And you're welcome!