Skip to main content
Nesa Nurani
Community Expert
Community Expert
Jul 27, 2020 7:53 PM UTC
Answered

help with code

if(this.getField("field").checkThisBox(0, event.value=="4/8"));

How do I make it check the checkbox even if values are "5/8","6/8","7/8" and "8/8"?

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jul 28, 2020 10:14 AM UTC

You can combine both functions into a single calculation script, like this:

var v1 = this.getField("field1").valueAsString;
var f = this.getField("checkbox");
if (v1 == "4" || v1 == "5" || v1=="6" || v1=="7" || v1=="8") {
	f.checkThisBox(0, true);
} else f.checkThisBox(0, false);
event.value = v1 + "/8";

14 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jul 28, 2020 10:14 AM UTC

You can combine both functions into a single calculation script, like this:

var v1 = this.getField("field1").valueAsString;
var f = this.getField("checkbox");
if (v1 == "4" || v1 == "5" || v1=="6" || v1=="7" || v1=="8") {
	f.checkThisBox(0, true);
} else f.checkThisBox(0, false);
event.value = v1 + "/8";
Translate
Nesa Nurani
Community Expert
Community Expert
Jul 28, 2020 10:57 AM UTC

First of all guys I really appreciate yout help and sry I'm PITA.

Ok now on codes:

ls_rbls your code only check checkbox if value is 4/8

EDIT: also need to enter 5,6,7,8/8 twice to check it.

 

try67  your code also only  check checkbox if value is 4 and for values 5,6,7 and 8 I need to enter them twice cuz first time it actually uncheck checkbox and second time I enter value it check it.

 

EDIT2: In both codes only value of 4 (4/8) is working as intended.

Would it be easier if instead of checkbox it's radio button?

Translate
try67
Community Expert
Community Expert
Jul 28, 2020 11:24 AM UTC

That doesn't make sense. Did you edit the code in any way? Can you share the actual file with us?

Translate
ls_rbls
Community Expert
Community Expert
Jul 28, 2020 5:25 AM UTC

I'm sure there's a variety of different ways to do it with javascript, but you can easily get the job done like this:

 

 

var s = event.target.value;
var f = this.getField("checkbox");
if ( (s == "4/8")  || ( s == "5/8")  || ( s =="6/8") || ( s =="7/8") ||  ( s == "8/8") ) {f.checkThisBox(0, true)}
else f.checkThisBox(0, false);

 

 

Also make the checkbox read-only so the users don't mess with its check /unchecked status if they try to click on it.

Translate
Technical Generalist
Nesa Nurani
Community Expert
Community Expert
Jul 28, 2020 5:55 AM UTC

Hi, thanks for your help.Code is working only if I enter values manually it doesn't work when other code change values in that field. I put code in validate of my textfield?

Translate
ls_rbls
Community Expert
Community Expert
Jul 28, 2020 6:07 AM UTC

I apologize. I am using it as a custom calculation script of the field where users will enter the values, not a validation script.

 

Were you originally asking about a validation script instead?

 

 

Translate
Technical Generalist