Copy link to clipboard
Copied
Good afternoon everybody,
I am experimenting and learning with Adobe Acrbat Pro, Javascript Forms. Lately, I've been working on On/Off buttons that turn on and off other selected buttons.
I've construdted a code with checkboxes, which each one turns On the lower ones, and Off the higher ones. Therefore, clicking on any would always show the previews options checked, while the later off.
For example, Pressing on Button1 would keep Buttons2-5 off. Button3 turns one Button1-2 and off Button4-5. And Button5 would turn on Button1-4.
Thus, Button1 is:
var x = this.getField("Button1").valueAsString;
if (x.value == "1")
{
this.getField("Button2").value = "Off";
this.getField("Button3").value = "Off";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
}
else
{
this.getField("Button2").value = "Off";
this.getField("Button3").value = "Off";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
}
Thus, Button3 is:
var x = this.getField("Button2").valueAsString;
if (x.value == "1")
{
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
}
else
{
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
}
Thus, Button5 is:
var x = this.getField("Button5").valueAsString;
if (x.value == "1")
{
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button3").value = "1";
this.getField("Button4").value = "1";
}
else
{
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button3").value = "1";
this.getField("Button4").value = "1";
}
So when I just left the If statement without the Else, it just breaks the code. Not having an If statement nor Else, also behaves that way.
Something, I'd like is that when the Checkbox is turned off it turns off all checkboxes, but it affects the On statement, keeping it turned off.
My question: Why does it happen that way? is there another way of doing it without so many lines? and last, how could one make it that the Off state turns off the other Checkboxes without affecting the On state?
No need to hurry, I'm just trying to learn and understand, and do love your input, opinions, help and explanations.
THANK YOU SO MUCH!
I've simplified your code and made it more generic. Place this code as a doc-level script:
function toggleFields() {
var allFields = ["Button1", "Button2", "Button3", "Button4", "Button5"];
var beforeCurrentField = true;
for (var i in allFields) {
if (allFields==event.target.name) {beforeCurrentField = false; continue;}
if (event.target.value=="Off" || beforeCurrentField==false) this.getField(allFields).value = "Off";
else this.getField(allFields).value = "1";
}
}
A
...Copy link to clipboard
Copied
1. Your if and else seem to be identical in each case. That is not wrong, but it's pointless.
2. Please explain "breaks the code".
Copy link to clipboard
Copied
Yes, I tried a few ways to avoid the else statement, but can't figured out how.
Without Else each checkbox only turns on itself. So just like this:
var x = this.getField("Button3").valueAsString;
if (x.value == "1")
{
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
}
Changing the Else Statment to all OFF, turns off every other Checkbox, no matter if the button was checked on or off. Meaning if I press Button3, it turns off 1,2,4, and 5. This code looks like this:
var x = this.getField("Button3").valueAsString;
if (x.value == "1")
{
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
}
else
{
this.getField("Button1").value = "Off";
this.getField("Button2").value = "Off";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
}
Therefore, only by having a redundant if and else codes I am able to get that progressive On/Off on the buttons.
Hope this helps undestand what I meant.
Copy link to clipboard
Copied
- Is the export value of all of those fields "1"?
- Where did you place the codes?
Copy link to clipboard
Copied
Yes, the Export value is set to 1
Copy link to clipboard
Copied
The "else" part is not obligatory. If you get an error message when you remove it it means you've done something wrong.
Copy link to clipboard
Copied
Without any if or else statement is does the same as I had it - turning them On/Off prgressively in order.
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
I just thought if I left a code for when it's On it'd do as thought, whereas when it's Off, it would turn Off every checkbox.
Copy link to clipboard
Copied
What do you mean by "progressively"? Also, you didn't answer my question about where you placed the code.
Copy link to clipboard
Copied
Oh sorry, I missed that.
I place the code as a Button Up Action Javascript on each checkbox.
And progressively as in button1 turns on button1; button 2 turns on button1-2; button3 turns on button1-3; and so on. And if Button5 is checked, then clicking on Button4 turns off button5; clicking on button2 turns off button3-5, and so on. Thus, one would have only On the choice selected and all previous ones, and turning Off or the later options.
Copy link to clipboard
Copied
I don't understand. How can a button turn itself on or off? That doesn't make sense. Isn't that what the user is doing by clicking it?
Copy link to clipboard
Copied
Yes, the user selects the Checkbox that he wants, and that CB will check the Previous checkboxes as well as uncheck the ones that come next.
There are five checkboxes:
Button1
Button2
Button3
Button4
Button5
Whenever the user checks a box, that button affects the others:
Button1: unchecks Button2 through Button5
this.getField("Button2").value = "Off";
this.getField("Button3").value = "Off";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
Button2: Checks Button1, and unchecks Button3 through Button5
this.getField("Button1").value = "1";
this.getField("Button3").value = "Off";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
Button3: Checks Button1 and Button2, and unchecks Button3 through Button5
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button4").value = "Off";
this.getField("Button5").value = "Off";
Button4: Checks Button1 through Button3, and unchecks Button5
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button3").value = "1";
this.getField("Button5").value = "Off";
Button5: checks Button1 through Button4
this.getField("Button1").value = "1";
this.getField("Button2").value = "1";
this.getField("Button3").value = "1";
this.getField("Button4").value = "1";
It does it regardless of if the checkbox was checked or uncheck, it will affect the other buttons.
Now, what I was trying to do is that when the user turns Off one Checkbox, it turns off all of them.
However, what I'm getting is that by checking or unchecking any checkbox, the code will still turn on the ones before, and turn off the ones after. (Eg. Button1 comes before Button2; and Button3 comes after Button2).
Copy link to clipboard
Copied
I've simplified your code and made it more generic. Place this code as a doc-level script:
function toggleFields() {
var allFields = ["Button1", "Button2", "Button3", "Button4", "Button5"];
var beforeCurrentField = true;
for (var i in allFields) {
if (allFields==event.target.name) {beforeCurrentField = false; continue;}
if (event.target.value=="Off" || beforeCurrentField==false) this.getField(allFields).value = "Off";
else this.getField(allFields).value = "1";
}
}
And then simply call the function from the Mouse Up event of each field, like this:
toggleFields();