Assign Different Scripts for a Field based on Radio Button Choice

Is it possible to run a different script for a field based on radio button choice. I have a radio button with two choices and I have three fields (field 2, field 3, field 4) that have scripts to automatically add days to the date from another field (field 1). I want these three fields to run a different script based on my radio button choice.

For example, if radio button 1 is selected field 2 runs script 1 for that field; if radio button 2 is selected then field 2 runs script 2 for that field. Same thing for fields 3 & 4. Basically if radio button 1 is selected I want field 2 to add 90 days to the date entered in field 1; if radio button 2 is selected then field 2 should add 150 days to the date entered in field 1. Fields 3 & 4 will have their respective number of days to add. I already have the scripts for fields 2 - 4 but I don’t know how to write the script to be based on the radio button choice.

This example of how to get the index of radio buttons might help. https://jsfiddle.net/g08vsb9o/1/

<p>An example of how to get the selected index of radio buttons.</p>
<form id="choiceform">
  <fieldset>
    <legend>Make your choice:</legend>
    <dl>
      <dd>
        <input type="radio" name="choice" id="choice1" value="choice2">
        <label for="choice1">Choice 1</label>
      </dd>
      <dd>
        <input type="radio" name="choice" id="choice2" value="choice2">
        <label for="choice2">Choice 2</label>
      </dd>
      <dd>
        <input type="radio" name="choice" id="choice3" value="choice3">
        <label for="choice3">Choice 3</label>
      </dd>
      <dd>
        <input type="radio" name="choice" id="choice4" value="choice4">
        <label for="choice4">Choice 4</label>
      </dd>
    </dl>
  </fieldset>
</form>
<output id="result"></output>
function getIndex(inputs) {
  return Array.from(inputs).findIndex(
  	(input) => input.checked
  );
}
function showDetails(input) {
  const form = input.form;
  const label = document.querySelector(`[for=${input.id}]`);
  const result = document.querySelector("#result");
  result.innerHTML = `
  	value: ${input.value},
    text: ${label.innerText},
    index: ${getIndex(form.elements[input.name])}
  `;
}
function choiceChangeHandler(evt) {
  const el = evt.target;
  if (el.name === "choice") {
    showDetails(el);
  }
}

const form = document.querySelector("#choiceform");
form.addEventListener("change", choiceChangeHandler);

Thanks for your reply, but I’m not understanding how to input this code. Below are examples of my form with my 2 choices that I manually manipulated.


This is the script I use for the 3 fields

var date= util.scand(“dd-mmm-yyyy”, this.getField(“Last Theater Entry Date Field”).value);
date.setDate(date.getDate()+75)
event.value=util.printd(“dd-mmm-yyyy”,date)
if (getField(“Last Theater Entry Date Field”).value==“”)event.value=“”

I just change the number of days that I want added to the “Last Theater Entry Date” for the 3 fields. I want to write a script that will automatically run and change the number of days for the fields based on which option is selected for the “Primary Work Location” radio button.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.