• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Related Check Box Java Script to Change Background Color

Community Beginner ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

I have three related check boxes that are names the same thing but are outputting diffent values so they work like a radio button but can be unselected unlike a radio button. I am trying to do three things with a Java Script in each check box

1. Populate a date field when selected

2. Clear a date field when unselected

3. Change background color of check box when selected and change to transparent when unslected

 

However since the check boxes are named the same thing the other 2 check boxes change background color as well. I only want the one that was selected to change color. Here is my current script for one check box

 

if(event.target.value == "Yes"){

this.getField("DateField1").value = new Date();

}

if(event.target.value == "Off"){

this.resetForm(["DateField1"]);

}

if(event.target.value == "Yes"){

event.target.fillColor = color.green;

}

if(event.target.value == "Off"){

event.target.fillColor = color.transparent;

}

 

Do I have to name the associated check boxes different names and then add more script to clear the other two associated check boxes? Looking for any suggesting here. Check list is for a Pass/Fail/NA checksheet so checkbox colors will be Pass = Green / Fail = Red/ NA = Transparent when selected. (Also super new to Java Script)

TOPICS
JavaScript , PDF forms

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 18, 2022 Jun 18, 2022

Since you named all checkboxes same so they act as radio buttons, you need to access their individual widgets, 1st checkbox has widget 0, 2nd checkbox is 1, 3rd is 2...etc, so for example lets say you named checkboxes "check" you would have to use like this:

this.getField("check.0"); or this.getField("check.1"); or this.getField("check.2");, for this example lets say you try to use script in 3rd checkbox, do it like this:

var f = this.getField("check.2");
if(f.valueAsString == "Yes"){
this.getFi
...

Votes

Translate

Translate
Community Expert ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

Since you named all checkboxes same so they act as radio buttons, you need to access their individual widgets, 1st checkbox has widget 0, 2nd checkbox is 1, 3rd is 2...etc, so for example lets say you named checkboxes "check" you would have to use like this:

this.getField("check.0"); or this.getField("check.1"); or this.getField("check.2");, for this example lets say you try to use script in 3rd checkbox, do it like this:

var f = this.getField("check.2");
if(f.valueAsString == "Yes"){
this.getField("DateField1").value = new Date();
f.fillColor = color.green;}

if(f.valueAsString == "Off"){
this.resetForm(["DateField1"]);
f.fillColor = color.transparent;}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

ok I got it to work in the first of 3 check boxes but the other two do not fill the date or change color when selected. I copied the script to each checkbox changing the widget number as you said. Any suggestions?

 

Also if the first check box is selected and turns green and then the second is selected without unslecting the first green box, the check moves to the second box as it should but the first box stays green and doesnt turn transparent. Any way to fix this?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

I figured out the first part. I forgot to change the script to match the export value in the second and third check boxes 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

To change background back to transparent you need to include it in script for other checkboxes because it's not calculation script it won't trigger automaticaly.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

LATEST

Thank you! This is my final script that I used for anyone elses reference.

var f = this.getField("Check.0");
if(f.valueAsString == "Pass"){
this.getField("DateField1").value = new Date();
f.fillColor = color.green;
this.getField("Check.1").fillColor = color.transparent;
this.getField("Check.2").fillColor = color.transparent;}

if(f.valueAsString == "Off"){
this.resetForm(["DateField1"]);
f.fillColor = color.transparent;}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines