Skip to main content
Participant
April 17, 2013
Answered

How to use JavaScript with Radio Buttons selected to assign a variable a value

  • April 17, 2013
  • 2 replies
  • 37457 views

Hi everyone, kinda new to Java and trying to build a form for my job for leave requests.  My issue is that I am trying to assign a variable a value whether one radio button or another is clicked.  Currently they are mutually exclusive with the following information: 

Name:  Radio

Tooltip:  Choice

Button Style:  Check

Radio Button Choice:  Yes (this is the export value I believe though I don't quite know how to use it properly)

neither are checked by default but if one is checked I would like to have a variable set to the text "Approved" and if the other radio button is checked I would like to set the variable to contain "Disapproved"

Here is my code currently and thanks in advance for the help to all:

var S_auth;

function auth() {

if (Radio.value == "Yes") {

var S_auth = "Approved";

  }

else {

var S_auth = "Disapproved";

  }

}

var Employee = this.getField("Emp_Name").value;

var Supervisor = this.getField("Supervisor").value;

var Leave_Type = this.getField("Dropdown2").value;

var cToAddr = name@emailaddy.com;

var cCCADDr = this.getField("Email").value;

var cBenAddr = this.getField("Sup_Email").value; if (cBenAddr != "") cCCADDr += ";" + cBenAddr;

var cSubline = "Re:  Leave request form for" + " " + Employee + ", " + Leave_Type + ", Status:  " + S_auth;

var cBody = "Your leave request has been " + S_auth + " by " + Supervisor.\n" + "Save mail attachment for your records.\n";

this.mailDoc({

     bUI:  true,

     cTo:  cToAddr,

     cCc:  cCCAddr,

     cSubject:  cSubLine,

     cMsg:  cBody

});

I honestly just want to have the subject line and body to be able to show approved or disapproved depending on which radio button the supervisor selects. 

This topic has been closed for replies.
Correct answer Anoop9178

Yes, your script is correct now and should work.

2 replies

Participating Frequently
September 28, 2016

Is there a way to do this with Radio buttons too? I tried the following, but when I do and fill out the form with one of the radio buttons listed, it keeps displaying the fields (all required even if there is a response) and thinks they are missing.

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

     var f= this.getField(this.getNthFieldName(i));

     if (f.type!="button" && f.required ) {

          if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off") || (f.type=="Radio" && f.value=="Off"))

 

f.strokeColor = color.red;   //Highlights the required fields in red

emptyFields.push(f.name);

    }

}

if (emptyFields.length>0) {

     app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));

}

else app.execMenuItem("Print");

try67
Community Expert
Community Expert
September 28, 2016

The code above does check the values of radio-buttons.

Participating Frequently
September 28, 2016

Ok. What I am finding though is that regardless if one of the radio button values are selected, the code is still coming up as requiring an answer for that particular required field. It doesn't seem to be working as intended.

Participant
April 17, 2013

if ( this.getField("Radio").value=="Yes") {

     S_auth = "Approved";

} else S_auth = "Disapproved";

I'm home from work and have been looking around but if anyone knows, will this work?  I'm going to try it in the morning

Anoop9178Correct answer
Participating Frequently
April 18, 2013

Yes, your script is correct now and should work.

Participant
April 18, 2013

When I press the submit button to create the email it still says the variable S_auth is undefined, is there something I did wrong?  I did what I wrote above and then tried this afterward and both of them state the variable is undefined:

First time

var S_auth;

function auth() {

if(this.getField("Radio").value == "Yes") {

S_auth = "Approved";

     }

else { S_auth = "Disapproved";

   }

}

Second time:

var Rad = this.getField("Radio").value;

var S_auth;

function auth() {

if(Rad == "Yes") {

var S_auth = "Approved";

     }

else {S_auth = "Disapproved";

  }

}

Now both times the variable S_auth gets printed as "undefined", is there something I'm doing wrong in getting the proper text to that variable?