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

How can I use a checkbox export value to copy text field value (text1) into another text field?

Community Beginner ,
May 01, 2020 May 01, 2020

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!

 

TOPICS
PDF forms

Views

4.1K

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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;
}

 

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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;
}

 

 MNHF_example.PNG

(note my checkbox is named "Rehire" - different name than text field).

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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?

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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 

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

Copy link to clipboard

Copied

Note: no document javascripts are used in this one

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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;
}

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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;
Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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 🙂

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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.

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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.

Votes

Translate

Translate

Report

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 ,
May 01, 2020 May 01, 2020

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.

Votes

Translate

Translate

Report

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
New Here ,
Mar 04, 2022 Mar 04, 2022

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. 

Votes

Translate

Translate

Report

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 ,
Mar 04, 2022 Mar 04, 2022

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.

Votes

Translate

Translate

Report

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
New Here ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

i do not see any error messages. here is the code in the Checkbox, 

 

if (event.target.value == "Yes")
{this.getfield("Name2").value = this.getfield("Name1").value;}
else {this.getfield("Name2").value = "";}

 

the Checkbox is named SP

Votes

Translate

Translate

Report

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 ,
Mar 04, 2022 Mar 04, 2022

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.

Votes

Translate

Translate

Report

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
New Here ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

that worked, thank you. 

Votes

Translate

Translate

Report

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 ,
Mar 04, 2022 Mar 04, 2022

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.

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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
New Here ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

that was the problem, thanks Thom. 

Votes

Translate

Translate

Report

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 ,
Mar 07, 2022 Mar 07, 2022

Copy link to clipboard

Copied

LATEST

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. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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