How do I write the JavaScript code so that when a radio button is checked, another one gets checked as well?

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: TrackerSupp-Daniel, Tracker Support, Chris - Tracker Supp, Vasyl-Tracker Dev Team, Sean - Tracker, Paul - Tracker Supp, Tracker Supp-Stefan, Ivan - Tracker Software

Post Reply
bram147
User
Posts: 2
Joined: Wed Sep 22, 2021 1:00 pm

How do I write the JavaScript code so that when a radio button is checked, another one gets checked as well?

Post by bram147 » Wed Sep 22, 2021 1:13 pm

Hey guys!

I'll get straight to the point. Basically I have a yes/no question and have put radio buttons near "Yes" and "No".

Now if the answer is "no", one should move on and read the document(nothing should happen). However, if the answer is "Yes", the radio button/check box at the end of the document should get checked automatically.

So, in my kind of ignorant beginner's view, at the first Radio Button's(the one near "Yes") Properties > Actions > On Focus, I should run a JavaScript code that basically says:

If Button near "Yes" is checked, then Button at the end should get checked.

I have very limited knowledge of JavaScript or how I could write this code. Any help?

Thanks in advance! :)

User avatar
Tracker Supp-Stefan
Site Admin
Posts: 14915
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: How do I write the JavaScript code so that when a radio button is checked, another one gets checked as well?

Post by Tracker Supp-Stefan » Wed Sep 22, 2021 1:46 pm

Hello bram147,

Please take a look at the attached file:
test_checkbox.pdf
(8.4 KiB) Downloaded 12 times
There's a radio button group, and when you click the "no" option ( the code is on the "on Focus" event for that) - it validates if the checkbox on the same page is already checked and if not - it adds the check mark in it.

Now you can improve on that and add more validations - e.g. ensure that the correct value of the radio button group is actually clicked and selected, and you can also e.g. make it so that selecting other of the radio options unchecks this box.

Kind regards,
Stefan

p.s. Actually - here's another version of the file - where the first radio option now hides the check mark and the second adds it:
check_uncheck_checkbox.pdf
(8.42 KiB) Downloaded 7 times

bram147
User
Posts: 2
Joined: Wed Sep 22, 2021 1:00 pm

Re: How do I write the JavaScript code so that when a radio button is checked, another one gets checked as well?

Post by bram147 » Wed Sep 22, 2021 2:31 pm

Tracker Supp-Stefan wrote:
Wed Sep 22, 2021 1:46 pm
Hello bram147,

Please take a look at the attached file:
test_checkbox.pdf

There's a radio button group, and when you click the "no" option ( the code is on the "on Focus" event for that) - it validates if the checkbox on the same page is already checked and if not - it adds the check mark in it.

Now you can improve on that and add more validations - e.g. ensure that the correct value of the radio button group is actually clicked and selected, and you can also e.g. make it so that selecting other of the radio options unchecks this box.

Kind regards,
Stefan

p.s. Actually - here's another version of the file - where the first radio option now hides the check mark and the second adds it:
check_uncheck_checkbox.pdf
Thank you very much! That's very helpful!

How would I go about it if there weren't a checkbox at the end, but two radio buttons? How would the code change to call one button, not the whole field?

User avatar
Tracker Supp-Stefan
Site Admin
Posts: 14915
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: How do I write the JavaScript code so that when a radio button is checked, another one gets checked as well?

Post by Tracker Supp-Stefan » Thu Sep 23, 2021 8:07 am

Hello bram147,

It is the state of the radio button group that controls the checkbox.
Or is it that you want one radio button group to control another radio button group?

Please take a look here for the full JS API documentation:
https://opensource.adobe.com/dc-acrobat ... apiref.pdf
And there is a dedicated section on form fields and what you can do with them.

Kind regards,
Stefan

Post Reply