Skip to main content
Known Participant
Nov 15, 2020 9:00 AM UTC
Answered

Need help with javascript

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.
Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
Nov 15, 2020 5:15 PM UTC

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
Nov 15, 2020 10:02 AM UTC

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;
}
Translate
MarietaaAuthor
Known Participant
Nov 15, 2020 10:12 AM UTC

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?

Translate
try67
Community Expert
Community Expert
Nov 15, 2020 10:17 AM UTC

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!

Translate