Skip to main content
Nesa Nurani
Community Expert
Community Expert
July 27, 2020
Answered

help with code

  • July 27, 2020
  • 2 replies
  • 2696 views
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 has been closed for replies.
Correct answer try67

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";

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 28, 2020

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";
Nesa Nurani
Community Expert
Community Expert
July 28, 2020

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?

try67
Community Expert
Community Expert
July 28, 2020

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

ls_rbls
Community Expert
Community Expert
July 28, 2020

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.

Nesa Nurani
Community Expert
Community Expert
July 28, 2020

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?

ls_rbls
Community Expert
Community Expert
July 28, 2020

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?