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

Javascript for checking boxes

Community Beginner ,
Sep 20, 2023 Sep 20, 2023

Hi everyone,

 

i got a pdf and want to let a textbox get deceided by checkboxes in front.

i got 5 questions where you can answer yes / no with 1 checkbox for every question:

 

The textbox should say "keine wesentliche Änderung" in those cases:

1. if checkbox 1 is a no + checkbox 2 is a no

2. if checkbox 1 is a no, checkbox 2  is a yes, checkbox 4 a yes

3. if checkbox 1 is a no, checkbox 2  is a yes, checkbox 4 a no + checkbox 5 a yes

 

The textbox should say "wesentliche Änderung" in those cases:

4. if checkbox 1 is a yes + checkbox 3 a no

5. if checkbox 1 is a yes + checkbox 3 a yes + checkbox 4 a yes

6. if checkbox 1 is a yes + checkbox 3 a yes + checkbox 4 a no + checkbox 5 a no

 

i tried to get the value with this.getField().value but it didnt work.

 

TIA Tim

TOPICS
JavaScript
2.4K
Translate
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 ,
Sep 20, 2023 Sep 20, 2023

> i tried to get the value with this.getField().value but it didnt work.

 

That should have worked (assuming you specified the field name as the parameter of getField)... A check-box group will have the value "Off" if no box is selected, or the export value of the selected box. What happened when you used that code?

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

the code is:

 

// Definition von Variablen

var check1 = this.getField("Check Box1").value;
var check2 = this.getField("Check Box2").value;
var check3 = this.getField("Check Box3").value;
var check4 = this.getField("Check Box4").value;
var check5 = this.getField("Check Box5").value;
var check6 = this.getField("Check Box6").value;

var Ergebnis = this.getField("Ergebnis").value;

// Anweisung

if
(check1 !== "Off" && check2 !== "Off") {
Ergebnis = "keine wesentliche Änderung"; }

 

nothing happened in textbox "ergebnis"

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

OK, that's because you're not applying the value correctly. Once you copy the value to a variable it's just a string, unattached to the field. Change the last lines to this:

 

var Ergebnis = this.getField("Ergebnis");

// Anweisung

if (check1 !== "Off" && check2 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung"; }
Translate
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 ,
Sep 20, 2023 Sep 20, 2023

î changed the code as you said to:

 

// Definition von Variablen

var check1 = this.getField("Check Box1").value;
var check2 = this.getField("Check Box2").value;
var check3 = this.getField("Check Box3").value;
var check4 = this.getField("Check Box4").value;
var check5 = this.getField("Check Box5").value;
var check6 = this.getField("Check Box6").value;

var Ergebnis = this.getField("Ergebnis");

// Anweisung

if (check1 !== "Off" && check2 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung"; }

 

but still nothing happens in the last textbox (see pictur)JS Error.pngexpand image

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

Where does you use the script?

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

- Where did you place the code?

- Did you check the JS Console for error messages?

- Did you change the value of any field after applying the code?

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

JS Error 2.pngexpand image

i placed the code behind Check Box 2.

JS Console told me syntax error:

// Definition von Variablen

var check1 = this.getField("Check Box1").value;
var check2 = this.getField("Check Box2").value;
var check3 = this.getField("Check Box3").value;
var check4 = this.getField("Check Box4").value;
var check5 = this.getField("Check Box5").value;
var check6 = this.getField("Check Box6").value;

var Ergebnis = this.getField("Ergebnis").value;

// Anweisung

if
(check1 !== "Off" && check2 !== "Off") {
Ergebnis = "keine wesentliche Änderung"; }
SyntaxError: syntax error
1:Console:Exec
undefined

Value of the Check Buttons is always Yes and No for every check box.

 

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

maybe it has something to do with that:

i got 2 checkboxes for every question and they name after the following strucute:

Question 1: yes (Name of Checkbox is: Check Box1#0) no (Name of Checkbox is Check Box1#1

Question 2: yes (Name of Checkbox is: Check Box2#0) no (Name of Checkbox is Check Box2#1 ...

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

Use the script at calculation of the textbox.

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

this nothing happens

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

Any error in the console?

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

I guess the problem is the value of 2 Checkbox if they have the same name e.g. Checkbox 1

I tried a test where Checkbox 7 and Checkbox 8 fill TextBox8 if they are both checked and it worked.

The problem is that i wanted a separat checkbox for yes and no which excludes the respective other.

Now somebody can theoretical select both yes and now for one question and it wouldnt work

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

When you use checkboxes with the same name and different export values only one checkbox can be selected.

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

thanks Bernd that was the plan that i wanted to have 2 checkboxes both with the name checkbox 1#0 and checkbox 1#1 so that just one can be selected.

I got this as a solution:

 

// Definition von Variablen
var check1 = this.getField("Check Box1").value;
var check2 = this.getField("Check Box2").value;
var check3 = this.getField("Check Box3").value;
var check4 = this.getField("Check Box4").value;
var check5 = this.getField("Check Box5").value;
var check6 = this.getField("Check Box6").value;
var check7 = this.getField("Check Box7").value;
var check8 = this.getField("Check Box8").value;
var check9 = this.getField("Check Box9").value;
var check10 = this.getField("Check Box10").value;

var Ergebnis = this.getField("Ergebnis");

// Überprüfe, ob Checkbox 2, Checkbox 3 und Checkbox 7 gleichzeitig ausgewählt sind
if (check2 !== "Off" && check3 !== "Off" && check7 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung";
} else if (check2 !== "Off" && check4 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung";
} else if (check1 !== "Off" && check6 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung";
} else if (check1 !== "Off" && check5 !== "Off" && check7 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung";
} else if (check2 !== "Off" && check3 !== "Off" && check8 !== "Off" && check9 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung";
} else if (check1 !== "Off" && check5 !== "Off" && check8 !== "Off" && check9 !== "Off") {
Ergebnis.value = "keine wesentliche Änderung";
} else if (check1 !== "Off" && check5 !== "Off" && check8 !== "Off" && check10 !== "Off") {
Ergebnis.value = "wesentliche Änderung";
} else if (check2 !== "Off" && check3 !== "Off" && check5 !== "Off" && check8 !== "Off" && check10 !== "Off") {
Ergebnis.value = "wesentliche Änderung";
} else {
// Wenn keine der oben genannten Bedingungen erfüllt ist, leere das Textfeld
Ergebnis.value = "";
}

 

Guess its easier than getting checkboxes with exclude the other

Translate
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 ,
Sep 20, 2023 Sep 20, 2023

Change all instances of Ergebnis.value to event.value .

Translate
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 ,
Sep 21, 2023 Sep 21, 2023

// Definition von Variablen
var check1 = this.getField("Check Box1").value;
var check2 = this.getField("Check Box2").value;
var check3 = this.getField("Check Box3").value;
var check4 = this.getField("Check Box4").value;
var check5 = this.getField("Check Box5").value;
var check6 = this.getField("Check Box6").value;
var check7 = this.getField("Check Box7").value;
var check8 = this.getField("Check Box8").value;
var check9 = this.getField("Check Box9").value;
var check10 = this.getField("Check Box10").value;

var output_ergeb = this.getField("Ergebnis");

// Überprüfe, ob Checkbox1 den Exportwert "No" und Checkbox2 den Exportwert "No" haben
if (check1 == "No" && check2 == "No") {
this.getField("Ergebnis").value = "keine wesentliche Änderung";
}

 

where should i change it to event.value?

Translate
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 ,
Sep 21, 2023 Sep 21, 2023

Change this:

this.getField("Ergebnis").value = "keine wesentliche Änderung";

To:

event.value = "keine wesentliche Änderung";

Translate
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 ,
Sep 21, 2023 Sep 21, 2023
LATEST

i got it done with the following code:

 

var checkbox1 = this.getField("Check Box1");
var checkbox2 = this.getField("Check Box2");
var resultField = this.getField("Ergebnis");

if (checkbox1.value === "No" && checkbox2.value === "No") {
resultField.value = "keine wesentliche Änderung";
} else {
resultField.value = "";
}

 

Thanks for your Help @try67 

Translate
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