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

Concat text strings based on radio button

Explorer ,
Jan 22, 2024 Jan 22, 2024

Ok lets say I have a specific phrase:

 

The quick ____A___ fox ____B___ over the ___C___ dog.

 

All fields are always going to refer to specific text fields.

 

However, field "B" will change from the default text input "JUMPED" to a text field if the user selects a radio button. The radio button associated with the text field would change the concated phrase to include whatever they put into the text field.

I need to concat that specific phrase with those fields. I know concatenate:

var s = this.getField("FIELDA").valueAsString + " "
 + this.getField("FIELDB").valueAsString + " "
 + this.getField("FIELDC").valueAsString;
event.value = s.replace(/^, |, $/g, "");

I know I can input words or phrases within the quotation marks but how do I include the radio buttons modifier within that?

I hope this is not confusing.

TOPICS
How to , JavaScript
1.7K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Jan 24, 2024 Jan 24, 2024

Well I didn't include several bits, so my script was not complete. I was riffing off of your original script, and just showing how to use the radio buttons to select a text component.  I've included the missing bits below.  But it's still a pretty simple script.   And is essentially the same thing you've presented above, but code correct.

 

For example, the replace function in this line doesn't do anything. 

event.value = s.replace(/^, |, $/g, "");

However, this is not an error. The code just returns the value in "s", essentially ignoring the replace function.

 

Also, in the rewrite of the script below "//My Dumba$$ Script ". You have put an if statement inline with the string concatonation statement. This is incorrect syntax, and does report an error. 

 

I think you would find it helpful to learn about using the Console Window, which is where errors are reported.

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm  

 

If you'd like to learn a bit about scripting fields in Acrobat you'll find the basics discussed here: 

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

Here's the correct, and complete code for the Custom Calculation script. 

 

var strText = "The quick " + this.getField("FieldA").value + " fox ";

if(this.getField("Group5").value == "Choice1")
   strText += this.getField("FieldB.0").value;
else
   strText += this.getField("FieldB.1").value;

strText += " over the " + this.getField("FieldC").value + " dog";

// This line puts the concatonated string into the field value. 
event.value = strText;

 

 

 

 

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

View solution in original post

Translate
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 ,
Jan 22, 2024 Jan 22, 2024

Hi @DarthTormaim ,

 

What exactly do you need the radio buttons to do?

 

The issue with radio buttons is that they work in groups and one must always remains selected unless you reserve one of the radio buttons to reset everything when it is ticked (requires user intervention).

 

Checkboxes can be more flexible based off of the  "checked" or "unchecked" state and they are not restricted to be paired up with another group of checkboxes in order to get something done.

Translate
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
Explorer ,
Jan 22, 2024 Jan 22, 2024

One Radio button would tell the concat script to use the default text (ex. JUMPED), the other radio button would tell the concat script to use the text in a specific text input.

Translate
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 ,
Jan 22, 2024 Jan 22, 2024

++Edited reply

 

Ok, I see.

 

Do you need just one group of radio buttons for field B or each field needs to have their own group of radio buttons?

Translate
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
Explorer ,
Jan 23, 2024 Jan 23, 2024

Just one group. Button A would be the default text and Button B would fill in the text from a textbox. 

 

I am updating a form that requires statements in a text block. Before I had the full statement and put a "______" and just overlayed 2 text inputs. The radio buttons would just hide one and unhide the other. I am trying to make these forms (42 pages total) smaller in size. So far I have been able to do that by 2/3. I just can't figure out this one. I really appreciate the help.

Translate
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 ,
Jan 23, 2024 Jan 23, 2024

The value of a radio button is the export of the checked button. 

You can read some about radio buttons and using them here:

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm 

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

 

You didn't provide the name of the radio button, or the export values. So I'll assume the exports are "A" and "B", and the radio button field name is "Radio".

 

var s = ((this.getField("Radio").value == "A")?this.getField("FIELDA").valueAsString:this.getField("FIELDB").valueAsString) + " "
 + this.getField("FIELDC").valueAsString;

 

Or to be more explicit it could be written like this:

var s = ((this.getField("Radio").value == "A")?this.getField("FIELDA").valueAsString:this.getField("FIELDB").valueAsString) + " "
 + this.getField("FIELDC").valueAsString;

 

 

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

Translate
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
Explorer ,
Jan 23, 2024 Jan 23, 2024

Ok I attached an example pdf. I hope this helps to illustrate what I'm trying to do. (P.S. Your website is a resource I have used many times. Thank you so much.)

Translate
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 ,
Jan 24, 2024 Jan 24, 2024

Hi @DarthTormaim ,

 

Here is your PDF with scipts on each radio button.  I think this is what you were inquiring about.

 

Let us know if the file linked below meets your requirements:

 

 

 

I eliminated the default value from FIELDB and did the following:

 

Custom calculation script for fieldA

 

 

this.getField("FIELDA").value = event.value;

 

 


Custom calculation script for fieldC

 

 

this.getField("FIELDC").value = event.value;

 

 

 

 

Radio Button Script for FieldB.0

 

 

var a = getField("fieldA");
var b0 = getField("FieldB.0");
var b1 = getField("FieldB.1");
var c = getField("fieldC");

if (a.value !=="" && b0.value !=="" && b1.value !=="" && c.value !=="") {

var s = "The quick " + this.getField("FIELDA").valueAsString +
" fox " + b0.valueAsString + " over the " +
this.getField("FIELDC").valueAsString;

this.getField("FIELDA").value = a.valueAsString;
this.getField("FIELDB").value = b0.valueAsString;
this.getField("FIELDC").value = c.valueAsString;
this.getField("Text1").value = s;

} else {

if (a.value =="" || b0.value =="" || b1.value =="" || c.value =="") {

this.getField("Text1").value = "";
  }
}

 

 

 

 

Radio Button script for FieldB.1

 

 

var a = getField("fieldA");
var b0 = getField("FieldB.0");
var b1 = getField("FieldB.1");
var c = getField("fieldC");

if (a.value !=="" && b0.value !=="" && b1.value !=="" && c.value !=="") {

var s = "The quick " + this.getField("FIELDA").valueAsString +
" fox " + b1.valueAsString + " over the " +
this.getField("FIELDC").valueAsString;

this.getField("FIELDA").value = a.valueAsString;
this.getField("FIELDB").value = b1.valueAsString;
this.getField("FIELDC").value = c.valueAsString;
this.getField("Text1").value = s;

} else {

if (a.value =="" || b0.value =="" || b1.value =="" || c.value =="") {

this.getField("Text1").value = "";
  }
}

 

 

 

Translate
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
Explorer ,
Jan 24, 2024 Jan 24, 2024

Perfect. If I may...

Can you tell me what this section is:

this.getField("FIELDA").value = a.valueAsString;
this.getField("FIELDB").value = b1.valueAsString;
this.getField("FIELDC").value = c.valueAsString;
this.getField("Text1").value = s;

 

and how the relationship with the below works? I'm not sure I understand. I'm literally trying to learn. Why point to the above if the fields below are providing the text to concat? Thank you very much for the help.

var a = getField("fieldA");
var b0 = getField("FieldB.0");
var b1 = getField("FieldB.1");
var c = getField("fieldC");

  

Translate
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 ,
Jan 24, 2024 Jan 24, 2024

ls_rbs, insteresting strategy, but quite a bit over complicated.  No radio button scripts are required.  This can all be done in a simple calculation script on the "example text block" field. 

 

DarthTormaim, if you use the script below, then any other scripts you've entered must be removed. 

 

var strText = this.getField("FieldA").valueAsString;

if(this.getField("Group5").value == "Choice1")
   strText += this.getField("FieldB.0").value;
else
   strText += this.getField("FieldB.1").value;

strText += this.getField("FieldC").value;


 

Set the field defaults to be underlines, i.e., "____"

 

 

 

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

Translate
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
Explorer ,
Jan 24, 2024 Jan 24, 2024

@Thom Parker I couldn't actually get yours to do anything. But I managed to isolate somthing here.

 

Here are my two scripts. Now I've tried inserting my "Textbox Tadio Button Choice" script right in my concat script where you would see: this.getField("FieldB.0").valueAsString

//Textbox Radio Button Choice
var a = this.getField("Group5").valueAsString;
var b = this.getField("FieldB.0").valueAsString;
var c = this.getField("FieldB.1").valueAsString;
if (a=="Choice1") event.value = b;
else if (a=="Choice2") event.value = c;
else event.value = "";

//Concat script
var s = "The quick " + 
this.getField("FieldA").valueAsString + 
" fox " + 
this.getField("FieldB.0").valueAsString + 
" over the " +
this.getField("FieldC").valueAsString +
" dog. ";
event.value = s.replace(/^, |, $/g, "");

//My Dumba$$ Script 

var a = this.getField("Group5").valueAsString;
var b = this.getField("FieldB.0").valueAsString;
var c = this.getField("FieldB.1").valueAsString;
var s = "The quick " + 
this.getField("FieldA").valueAsString + 
" fox " + {
if (a=="Choice1") event.value = b;
else if (a=="Choice2") event.value = c;
else event.value = "";
}
 + 
" over the " +
this.getField("FieldC").valueAsString +
" dog. ";
event.value = s.replace(/^, |, $/g, "");

 I've tried brackets and everything I could think of, googled it. I feel like I am right there at the precipice of figuring it out. Am I deluded or am I on the right track? I do get errors when I put that in there like that. 

Translate
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 ,
Jan 24, 2024 Jan 24, 2024

Well I didn't include several bits, so my script was not complete. I was riffing off of your original script, and just showing how to use the radio buttons to select a text component.  I've included the missing bits below.  But it's still a pretty simple script.   And is essentially the same thing you've presented above, but code correct.

 

For example, the replace function in this line doesn't do anything. 

event.value = s.replace(/^, |, $/g, "");

However, this is not an error. The code just returns the value in "s", essentially ignoring the replace function.

 

Also, in the rewrite of the script below "//My Dumba$$ Script ". You have put an if statement inline with the string concatonation statement. This is incorrect syntax, and does report an error. 

 

I think you would find it helpful to learn about using the Console Window, which is where errors are reported.

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm  

 

If you'd like to learn a bit about scripting fields in Acrobat you'll find the basics discussed here: 

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

Here's the correct, and complete code for the Custom Calculation script. 

 

var strText = "The quick " + this.getField("FieldA").value + " fox ";

if(this.getField("Group5").value == "Choice1")
   strText += this.getField("FieldB.0").value;
else
   strText += this.getField("FieldB.1").value;

strText += " over the " + this.getField("FieldC").value + " dog";

// This line puts the concatonated string into the field value. 
event.value = strText;

 

 

 

 

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

Translate
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 ,
Jan 24, 2024 Jan 24, 2024

Amazing!  And simple to understand.

 

I thought of doing it as custom calculation script too, and I was also thinking that a dropdown menu could've worked too instead of employing radio buttons for field B.

 

Thanks!

Translate
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 ,
Jan 25, 2024 Jan 25, 2024
LATEST

You are correct. A dropdown field would also work.  

 

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

Translate
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