Javascript for checking boxes
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
> 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?
Copy link to clipboard
Copied
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"
Copy link to clipboard
Copied
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"; }
Copy link to clipboard
Copied
î 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)
Copy link to clipboard
Copied
Where does you use the script?
Copy link to clipboard
Copied
- 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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 ...
Copy link to clipboard
Copied
Use the script at calculation of the textbox.
Copy link to clipboard
Copied
this nothing happens
Copy link to clipboard
Copied
Any error in the console?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
When you use checkboxes with the same name and different export values only one checkbox can be selected.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Change all instances of Ergebnis.value to event.value .
Copy link to clipboard
Copied
// 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?
Copy link to clipboard
Copied
Change this:
this.getField("Ergebnis").value = "keine wesentliche Änderung";
To:
event.value = "keine wesentliche Änderung";
Copy link to clipboard
Copied
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

