Home > Net >  How to hide/show div on radio button change
How to hide/show div on radio button change

Time:01-13

I am trying to hide two rows on radio button change. I have a question (Is your postal address the same as your residential address), Asking user with radio buttons labelled YES/NO If the user check YES the two rows remain unchecked, if they check NO the div will be displayed to them now all working fine but when I check NO but it displayed it like this enter image description here

but it is supposed to show me like this enter image description here Here is my code `

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="keywords" content="HTML5 Template" />
  <meta name="description" content="Webmin - Bootstrap 4 & Angular 5 Admin Dashboard Template" />
  <meta name="author" content="potenzaglobalsolutions.com" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>Recruitment</title>

  <!-- online Bootsrtap -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3 Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  <style>
    /*a:link {
    text-decoration: underline;
  }
  a:hover {
    text-decoration: none;
    }*/
    
    input[type='radio'] {
      transform: scale(2);
    }
    
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
      -webkit-appearance: none;
      margin: 0;
    }
  </style>


</head>

<body>
  <div >
    <div >
      <div >
        <div  style="margin-left:0px">
          <div >
            <div >
              <div  style="text-align:center;font-size: 25px;font-family: sans-serif;">
                <h4 >Application Form</h4>
              </div>
            </div>
          </div>
          <br/>
          <br/>
          <div >
            <div >
              <div >
                <div >
                  <label  for="emailAddr"><b>Current Email Address</b></label>
                </div>
                <div >
                  <input required type="email" style="width:300px"  id="emailAddr">
                </div>
                <div >
                  <label  for="phonenum"><b>Mobile number</b></label>
                </div>
                <div >
                  <input required type="number" style="width:300px"  id="phonenum">
                </div>
              </div>
              <br><br>
              <div >
                <div >
                  <label  for="surname"><b>Surname</b></label>
                </div>
                <div >
                  <input required type="text" style="width:300px" autocomplete="off"  id="surname" name="surname" />
                </div>
                <div >
                  <label  for="givenname"><b>Given name</b></label>

                </div>
                <div >
                  <input required type="text" style="width:300px" autocomplete="off"  id="givenname" name="givenname" />
                </div>
              </div>
              <br><br>
              <div >
                <div >
                  <label  for="preferredname"><b>Preferred name</b></label>

                </div>
                <div >
                  <input required type="text" style="width:300px" autocomplete="off"  id="preferredname" name="preferredname">
                </div>
                <div >
                  <label  for="streetaddress"><b>Street address</b></label>

                </div>
                <div >
                  <input required type="text" style="width:300px" autocomplete="off"  id="streetaddress" name="streetaddress">
                </div>
              </div>
              <br><br>
              <div >
                <div >
                  <label  for="status"><b>State</b></label>
                </div>
                <div >
                  <select  style="width:300px" id="state" name="state">
                    <option value="0">Select State</option>
                    <option value="1">QLD</option>
                    <option value="2">NSW</option>
                    <option value="3">VIC</option>
                    <option value="4">TAS</option>
                    <option value="5">ACT</option>
                    <option value="6">WA</option>
                    <option value="7">SA</option>
                    <option value="8">NT</option>
                  </select>
                </div>
                <div >
                  <label  for="postcode"><b>Postcode</b></label>
                </div>
                <div >
                  <input required type="number" style="width:300px" autocomplete="off"  id="postcode" name="postcode">
                </div>
              </div>
              <br><br>
              <div >
                <div >
                  <label  for="postalcheck"><b>Is your postal address the same as your residential address</b></label>
                </div>
                <div >
                  <div >
                    <input  type="radio" name="inlineRadioOptions" id="inlineRadio1" value="1" onchange="getValue(this)">
                    <label  for="inlineRadio1">&emsp;<b>Yes</b></label>
                  </div>
                  <div >
                    <input  type="radio" name="inlineRadioOptions" id="inlineRadio2" value="2" onchange="getValue(this)">
                    <label  for="inlineRadio2">&emsp;<b>No</b></label>
                  </div>
                </div>
                <div >
                  <label  for="resume"><b>Upload a recent copy of your resume</b></label>
                </div>
                <div >
                  <input type="file" id="resume" name="resume">
                </div>
              </div>
              <br><br>
              <div  id="postalone">
                <div >
                  <label  for="postalstate"><b>Postal: State</b></label>
                </div>
                <div >
                  <select  style="width:300px" id="state" name="state">
                    <option value="0">Select Postal State</option>
                    <option value="1">QLD</option>
                    <option value="2">NSW</option>
                    <option value="3">VIC</option>
                    <option value="4">TAS</option>
                    <option value="5">ACT</option>
                    <option value="6">WA</option>
                    <option value="7">SA</option>
                    <option value="8">NT</option>
                  </select>
                </div>
                <div >
                  <label  for="postpostcode"><b>Postal: Postcode</b></label>
                </div>
                <div >
                  <input required type="text" style="width:300px"  id="postpostcode" name="postpostcode">
                </div>
              </div>
              <br><br>
              <div  id="postaltwo">
                <div >
                  <label  for="poststraddr"><b>Postal: Street address</b></label>
                </div>
                <div >
                  <input required type="text" style="width:300px"  id="poststraddr" name="poststraddr">
                </div>
              </div>
              <br><br>
              <p ></p>
              <br><br>
              <div >
                <div >
                  <h3>Screening Questions</h3>
                </div>
              </div>
              <br><br>
              <div >
                <div >
                  <label  for="postalstate"><b>Q1</b></label>
                </div>
                <div >
                  <input required type="text" style="width:300px"  id="postalstate" name="postalstate" />
                </div>
                <div >
                  <label  for="fname"><b>Q2</b></label>
                </div>
                <div >
                  <input required type="text" style="width:300px"  id="LevelOfExperience" name="LevelOfExperience" />
                </div>
              </div>
              <br><br>
              <div >
                <div >
                  <label  for="postalstate"><b>Q3</b></label>
                </div>
                <div >
                  <input required type="text" style="width:300px"  id="postalstate" name="postalstate" />
                </div>
                <div >
                  <label  for="fname"><b>Q4</b></label>
                </div>
                <div >
                  <input required type="text" style="width:300px"  id="LevelOfExperience" name="LevelOfExperience" />
                </div>
              </div>
              <br><br>
              <p ></p>
              <br>
              <div >
                <div ></div>
                <div >
                  <input type="button"  value="Apply" onclick="SaveApplication();">
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  </div>


  <!-- online bootstrap -->
  <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2 poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ n" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft 2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

  <script>
    function getValue(radio) {
      if ((radio.value) == 1) {
        document.getElementById("postalone").style.display = "none";
        document.getElementById("postaltwo").style.display = "none";
      } else {
        document.getElementById("postalone").style.display = "block";
        document.getElementById("postaltwo").style.display = "block";
      }
    }
  </script>
</body>

</html>

`

CodePudding user response:

Remove the style tag from your columns and use a single div tag and put all inputs in side that. then use the code to hide the div.

<div id="postal" style="display: none;" >   
    <div  >
        <div >
        <label  for="postalstate"><b>Postal: State</b></label>
        </div>
        <div >
            <select  style="width:300px" id="state" name="state">
            <option value="0">Select Postal State</option>
            <option value="1">QLD</option>
            <option value="2">NSW</option>
            <option value="3">VIC</option>
            <option value="4">TAS</option>
            <option value="5">ACT</option>
            <option value="6">WA</option>
            <option value="7">SA</option>
            <option value="8">NT</option>
            </select>
        </div>
        <div >
        <label  for="postpostcode"><b>Postal: Postcode</b></label>  
        </div>
        <div >
        <input required type="text" style="width:300px"  id="postpostcode" name="postpostcode">
        </div>
    </div>


    <br><br>

    <div  id="postaltwo">      
        <div >
        <label  for="poststraddr"><b>Postal: Street address</b></label>
        </div>
        <div >
        <input required type="text" style="width:300px"  id="poststraddr" name="poststraddr">
        </div>
    </div>
</div>

use less javascript code.

function getValue(radio) {
    if ((radio.value) == 1) {
        document.getElementById("postal").style.display = "none";
    } else {
        document.getElementById("postal").style.display = "block";
    }
}
  •  Tags:  
  • Related