Skip to main content
DarthTormaim
Known Participant
January 23, 2024
Answered

Concat text strings based on radio button

  • January 23, 2024
  • 2 replies
  • 2504 views

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.

This topic has been closed for replies.
Correct answer Thom Parker

@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. 


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;

 

 

 

 

2 replies

ls_rbls
Community Expert
Community Expert
January 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 = "";
  }
}

 

 

 

Thom Parker
Community Expert
Community Expert
January 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
DarthTormaim
Known Participant
January 25, 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. 

ls_rbls
Community Expert
Community Expert
January 23, 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.

DarthTormaim
Known Participant
January 23, 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.

ls_rbls
Community Expert
Community Expert
January 23, 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?