Skip to main content
Known Participant
April 23, 2025
Question

Radio Button Changes Default Value of Text Box

  • April 23, 2025
  • 4 replies
  • 829 views

I have a text field with a default value, which tells the Customer what info needs to go into that field. In this text field, I have "On Focus" and "On Blur" actions to have the default value grey, but anything the Customer writes, overwrites the default value (without the Customer having to delete it), and then their text is black. This text field is working great.

 

However, when one radio button is selected (out of two, it's only one or the other), I want the same functionality, but a different default value. For example, the text field's default value is currently "Company Name", but when that one radio button is on, I want the default value of that text field to change to "Order#" (with the Customer being able to overwrite it, without having to delete anything, changing the color from grey to black). 

 

I tried writing a "Mouse Up" Javascript under the radio button itself to have that text field change to "Order#" when on, but then the Customer has to delete "Order#" to write their order#, which is annoying. And I also tried the code mentioned in the below article (the one for the Custom Calculation in the text field), but I couldn't get it to work at all (maybe some conflicting codes?...) and I'm not sure it's really what I need. 

 

https://community.adobe.com/t5/acrobat-discussions/overwrite-a-text-box-populated-based-on-radio-button-checking/td-p/14914560 

 

Thanks in advance for taking the time! Hoping this one is easy for you guys 🙂

4 replies

JR Boulay
Community Expert
Community Expert
April 23, 2025

Be sure that the 2 checkboxes shares exactly the same name and have a different export value.

"Off" means: unchecked, this is available for checkboxes and radiobuttons only. Not for text fields.

Contrary to popular belief, “reset” doesn't empty fields, but rather resets them to their default values. So before the reset you must add:

this.getField("TEXT-FIELD-NAME").defaultValue = "";

 

See attachement.

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
Community Expert
April 23, 2025

"Off" is the value returned if a (set of) checkbox(es) is not checked.

Replace Yes/No by the true values of the checkboxes.

Replace CHECKBOX-NAME and TEXT-FIELD-NAME by true names.

 

Use this:

// On Blur:
if (event.target.value=="") {
var ckbox = this.getField("CHECKBOX-NAME");
if (ckbox.value === "Off") {event.target.defaultValue = "Company Name";}
else if (ckbox.value === "Yes") {event.target.defaultValue = "Order#";}
else if (ckbox.value === "No") {event.target.defaultValue = "Blablabla";}
event.target.value = event.target.defaultValue;
event.target.textColor = color.ltGray;
}

// Mouse up action in the 2 checkboxes:
var ffield = this.getField("TEXT-FIELD-NAME");
if (event.target.value === "Off") {ffield.defaultValue = "Company Name";}
else if (event.target.value === "Yes") {ffield.defaultValue = "Order#";}
else if (event.target.value === "No") {ffield.defaultValue = "Blablabla";}

ffield.value = ffield.defaultValue;

 

Acrobate du PDF, InDesigner et Photoshopographe
Known Participant
April 23, 2025

Thanks @JR Boulay !

That is working for me. One last question/request: do the below lines from the codes mean 'when neither of the 2 radioboxes are checked' ?  Currently, when I click the "Reset" button, the default text that was previously there before clicking Reset stays there (even though no radio button is checked). I'd like the default text to disappear when no radio button is checked (when the form is first opened or when the Reset button is clicked).  I tried writing {event.target.defaultValue = "";} and {ffield.defaultValue = "";} next to those 2 below lines, but that didn't work. Thanks again!

 

if (ckbox.value === "Off")

if (event.target.value === "Off")

JR Boulay
Community Expert
Community Expert
April 23, 2025

Try this:

// On Blur:
if (event.target.value=="") {
if (this.getField("CHECKBOX-NAME").value != "Off") {event.target.defaultValue = "Order#";}
else {event.target.defaultValue = "Company Name";}
event.target.value = event.target.defaultValue;
event.target.textColor = color.ltGray;
}
Acrobate du PDF, InDesigner et Photoshopographe
Known Participant
April 23, 2025

Thanks @JR Boulay !

Just to confirm, where you have "CHECKBOX-NAME", I write the general name of my 2 radio buttons, right? And where you have "Off", I write one of my radio button choice names, right? When I did that, I got the code to work exactly as I asked it to.

 

But now I'm wondering if I can still fix one more thing. Might be a completely different code... Right now, when I click on one of the radio buttons, the text field is blank (no default value) and I'm guessing that's because the code only works in the text field after Customer leaves it blank (only then do you see the requested default value from the code you provided). But I would prefer that the Customer see the correct default value ("Order#" for choice1 and "Company Name" for choice2) immediately upon clicking the radio button, instead of only seeing the default value if they leave that field blank. Would that be possible? Thanks so much!

Nesa Nurani
Community Expert
Community Expert
April 23, 2025

What script do you use 'On Focus' and 'On Blur'?

Known Participant
April 23, 2025

Hi Nesa,

The On Focus is:

if (event.target.value==event.target.defaultValue) {
event.target.value = "";
event.target.textColor = color.black;
}

 

And the On Blur is:

if (event.target.value=="") {
event.target.value = event.target.defaultValue;
event.target.textColor = color.ltGray;
}

 

Thx!

 

Known Participant
April 23, 2025