Copy link to clipboard
Copied
I have mutually exclusive checkboxes...one with export value of No, and the other with export value of Yes.
Nothing happens if no checkbox is selected - but if the Yes checkbox is selected, I want javascript that will copy the value of text1 into text2 and make text2 read-only.
If the yes box is un-checked, I want text 2 to be an editable text field again.
Can anyone help me? I've searched and modified few different scripts I found online, but nothing is working 100%.
Thanks in advance for any help!
Copy link to clipboard
Copied
As the Mouse Up event of the check-box fields enter this code:
if (event.target.value=="Yes") {
this.getField("text 2").value = this.getField("text 1").value;
this.getField("text 2").readonly = true;
} else {
this.getField("text 2").value = "";
this.getField("text 2").readonly = false;
}
Copy link to clipboard
Copied
Thank you @Try67...when I use the code, I can see that something is happening because when I test and check the "Yes" checkbox, my field shading for text2 goes away, but the actual text from text1 is not showing?
Can you review and advise if I have an error - or another suggestion? Thank you!!
Here's the code (modifed yours with my actual field names):
//If person is rehire, copy rehire EEID to STN_EEID text field, and make Read-only
if (event.target.value=="Yes") {
this.getField("STN_EEID").value = this.getField("Rehire_STN_EEID").value;
this.getField("STN_EEID").readonly = true;
} else {
this.getField("STN_EEID").value = "";
this.getField("STN_EEID").readonly = false;
}
(note my checkbox is named "Rehire" - different name than text field).
Copy link to clipboard
Copied
The code looks fine... However, the Rehire_STN_EEID field seems empty. Did you enter something into it before trying it? Can you share the actual file with us?
Copy link to clipboard
Copied
The code provided by try copies the field value when the "Yes" button is pressed, and only then. If the "Rehire_STN_EEID" field contains a value, then it should be copied at that time. But changing the "Rehire_STN_EEID" field value will not change the "STN_EEID" field.
if you want the "STN_EEID" field to automatically update, you'll need to used a Calculation script on the "STN_EEID" field.
Copy link to clipboard
Copied
Thank you ...I think I understand what you're both saying...and when I tested, I checked the Yes checkbox and then typed in a number in the Rehire_STN_EEID field - but nothing showed up.
Thom - I tried using a calculation script (might have been a validation script) yesterday, that would check if Rehire_STN_EEID has a value, and if it does, to populate STN_EEID...but I couldn't get this to work. I'm flexible with "how" this works...ultimately, I just need the employee ID number for any rehires (where "Yes" checkbox is checked) to populate the STN_EEID and then make it read-only.
Here's the link to my PDF (hope it works)
https://drive.google.com/file/d/1FqCiPi5oBNPfUS_xn_t3EtlQDblbo7YB/view?usp=sharing
Copy link to clipboard
Copied
Note: no document javascripts are used in this one
Copy link to clipboard
Copied
Works fine for me. However, as Thom pointed out, the code I provided you with will only copy the value at the moment Yes is clicked. If you enter it after clicking Yes it will not be copied.
If you want it to work like that then you can use the following code as the custom Calculation script of STN_EEID (and remove the MouseUp code from the check-boxes, of course):
if (this.getField("Rehire").value=="Yes") {
event.value = this.getField("Rehire_STN_EEID").value;
event.target.readonly = true;
} else {
event.target.readonly = false;
}
Copy link to clipboard
Copied
I would write the overide calculatin code differently.
event.rc = (this.getField("Rehire").value=="Yes");
event.value = this.getField("Rehire_STN_EEID").value;
event.target.readonly = event.rc;
Copy link to clipboard
Copied
Thank you so much! This is working (both methods provided) - the only thing I noticed is if I clear text in the Rehire_STN_EEID field, the value does not clear from the STN_EEID field.
Is there something I can add to the calculation script, or would I need to add a validation script to check the Rehire_STN_EEID field again?
Still a beginner with javascript - but all my fillable PDF forms have been built over the years with help from both of you (without you knowing it, through your posts in this other forums). You're both very good and Really appreciate your help 🙂
Copy link to clipboard
Copied
If the "Yes" option is selected it will clear it. When it's not you will need to do it manually, as the values are no longer connected.
Copy link to clipboard
Copied
ok - understood, thanks! And here I thought anything was possible with javascript...there are limits after all, lol. Have a good weekend.
Copy link to clipboard
Copied
It's not impossible, but it will require a more complex script, one in which you will need to check which field triggered the event.
Copy link to clipboard
Copied
hi, try67
i am using exact code that you provided, it is in the Checkbox field, MouseUp and Run a Java Script. I would like copy Name1 to Name2 when the Checkbox is clicked ( the Export value is "Yes"). My problem is after i entered value in the Name1, clicked the Checkbox, nothing populated. it doesn't seem to recognize the value of the Checkbox. Appreciate your help.
Copy link to clipboard
Copied
Double-check the field names, and also the JS Console for error messages.
If you can't see any errors, share the file for further help.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
There are errors in your code, so there should be error messages in the JS Console when you run it.
Press Ctrl+J to open the Console and you'll see them.
Namely, you used an incorrect method name, getfield, instead of getField.
Copy link to clipboard
Copied
that worked, thank you.
Copy link to clipboard
Copied
You have a spelling error in the code. "getField" is spelled with a capital "F". And this error is displayed in the console window. Press "Ctrl-J" to see the console window.
Copy link to clipboard
Copied
that was the problem, thanks Thom.
Copy link to clipboard
Copied
You are welcome 🙂 , however, had I seen try67's answer I would not have responded. For some reason I did not get the updated forum messages until long after they were posted. Perhaps a glitch in the system.