Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a form using radio buttons?

Using radio button I want to change my form. For example if 'A' radio button is selected, it should show me a form where I can enter my name and age. If 'B' is selected, it should show me another form, where I can see a picture, but the text field for the name and age are no longer visible.

like image 752
Sara Avatar asked Aug 06 '11 16:08

Sara


2 Answers

I liked the Answer from Vinayak Garg

Though a more portable solution was desired that could be used for many options using a basic structure minimal javascript is needed to swap options

The example function used in the next 2 snippets is:

function swapConfig(x) {
  var radioName = document.getElementsByName(x.name);
  for (i = 0; i < radioName.length; i++) {
    document.getElementById(radioName[i].id.concat("Settings")).style.display = "none";
  }
  document.getElementById(x.id.concat("Settings")).style.display = "initial";
}

function swapConfig(x) {
    var radioName = document.getElementsByName(x.name);
    for(i = 0 ; i < radioName.length; i++){
      document.getElementById(radioName[i].id.concat("Settings")).style.display="none";
    }
    document.getElementById(x.id.concat("Settings")).style.display="initial";
  }
<fieldset>
  <legend>Url and Domain Configuration</legend>
  <p>
    <label for="production">Production</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="production" checked="checked" />
    <label for="development">Development</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="development" />
  </p>
  <div id="productionSettings">
    <br/>Production Settings
    <p>
      <label for="p1">Production1</label>
      <input type="text" name="p1" value="/">
    </p>
  </div>
  <div id="developmentSettings" style="display:none">
    <br/>Development Settings
    <p>
      <label for="d1">Developent1</label>
      <input type="text" name="d1" value="/">
    </p>
  </div>
</fieldset>

Doing it this way you can add new options without changing the javascript such as adding alpha and beta options as shown below you will see the same javascript is used.

function swapConfig(x) {
  var radioName = document.getElementsByName(x.name);
  for (i = 0; i < radioName.length; i++) {
    document.getElementById(radioName[i].id.concat("Settings")).style.display = "none";
  }
  document.getElementById(x.id.concat("Settings")).style.display = "initial";
}
<fieldset>
  <legend>Url and Domain Configuration</legend>
  <p>
    <label for="production">Production</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="production" checked="checked" />
    <label for="development">Development</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="development" />
    <label for="alpha">Alpha</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="alpha" />
    <label for="beta">Beta</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="beta" />
  </p>
  <div id="productionSettings">
    <br/>Production Settings
    <p>
      <label for="p1">Production</label>
      <input type="text" name="p1" value="/">
      </p>
  </div>
  <div id="developmentSettings" style="display:none">
    <br/>Development Settings
    <p>
      <label for="d1">Developent</label>
      <input type="text" name="d1" value="/">
    </p>
  </div>
  <div id="alphaSettings" style="display:none">
    <br/>Alpha Settings
    <p>
      <label for="a1">Alpha</label>
      <input type="text" name="a1" value="/">
    </p>
  </div>
  <div id="betaSettings" style="display:none">
    <br/>Beta Settings
    <p>
      <label for="b1">Beta</label>
      <input type="text" name="b1" value="/">
      </p>
  </div>
</fieldset>

It could be even more reusable by adding a second variable to the function:

function swapConfig(x, y) {
  var radioName = document.getElementsByName(x.name);
  for (i = 0; i < radioName.length; i++) {
    document.getElementById(radioName[i].id.concat(y)).style.display = "none";
  }
  document.getElementById(x.id.concat(y)).style.display = "initial";
}

function swapConfig(x, y) {
      var radioName = document.getElementsByName(x.name);
      for (i = 0; i < radioName.length; i++) {
        document.getElementById(radioName[i].id.concat(y)).style.display = "none";
      }
      document.getElementById(x.id.concat(y)).style.display = "initial";
    }
<fieldset>
  <legend>Url and Domain Configuration</legend>
  <p>
    <label for="production">Production</label>
    <input type="radio" onchange="swapConfig(this, 'Settings')" name="urlOptions" id="production" checked="checked" />
    <label for="development">Development</label>
    <input type="radio" onchange="swapConfig(this,'Settings')" name="urlOptions" id="development" />
    <label for="alpha">Alpha</label>
    <input type="radio" onchange="swapConfig(this,'Settings')" name="urlOptions" id="alpha" />
    <label for="beta">Beta</label>
    <input type="radio" onchange="swapConfig(this,'Settings')" name="urlOptions" id="beta" />
  </p>
  <p>
    <label for="alphaVar">Alpha</label>
    <input type="radio" onchange="swapConfig(this,'Val')" name="urlVars" id="alphaVar" checked="checked"  />
    <label for="betaVar">Beta</label>
    <input type="radio" onchange="swapConfig(this,'Val')" name="urlVars" id="betaVar" />
  </p>
  <div id="productionSettings">
    <br/>Production Settings
    <p>
      <label for="p1">Production</label>
      <input type="text" name="p1" value="/">
      </p>
  </div>
  <div id="developmentSettings" style="display:none">
    <br/>Development Settings
    <p>
      <label for="d1">Developent</label>
      <input type="text" name="d1" value="/">
    </p>
  </div>
  <div id="alphaSettings" style="display:none">
    <br/>Alpha Settings
    <p>
      <label for="a1">Alpha</label>
      <input type="text" name="a1" value="/">
    </p>
  </div>
  <div id="betaSettings" style="display:none">
    <br/>Beta Settings
    <p>
      <label for="d1">Beta</label>
      <input type="text" name="b1" value="/">
      </p>
  </div>
  <div id="alphaVarVal">
    <br/>Alpha Values
    <p>
      <label for="aV1">Alpha Vals</label>
      <input type="text" name="aV1" value="/">
      </p>
  </div>
  <div id="betaVarVal" style="display:none">
    <br/>Beta Values
    <p>
      <label for="bV1">Beta Vals</label>
      <input type="text" name="bV1" value="/">
      </p>
  </div>
</fieldset>

Javascript for loops are well described in this Answer of the question For-each over an array in JavaScript?

like image 28
CrandellWS Avatar answered Sep 29 '22 07:09

CrandellWS


You can use the onchange attribute of an input to call a javascript function, which hides A & shows B or vice versa

 function hideA(x) {
   if (x.checked) {
     document.getElementById("A").style.visibility = "hidden";
     document.getElementById("B").style.visibility = "visible";
   }
 }

 function hideB(x) {
   if (x.checked) {
     document.getElementById("B").style.visibility = "hidden";
     document.getElementById("A").style.visibility = "visible";
   }
 }
Show :
<input type="radio" onchange="hideB(this)" name="aorb" checked>A |
<input type="radio" onchange="hideA(this)" name="aorb">B
<div id="A">
  <br/>A's text</div>
<div id="B" style="visibility:hidden">
  <br/>B's text</div>
like image 123
Vinayak Garg Avatar answered Sep 29 '22 09:09

Vinayak Garg