Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Show and hidden radio button depending on Dropdown selection

    Dear Javascript programmers,

    Do you have an example of a Javascript something like this:

    A dropdownlist with values "one" and "two". The radio button is not visible (as a start).
    - If you have selected "one" from the dropdown list, then a radio button will appear.
    - If you have selected "two" from the dropdown list, then a radio button will NOT appear.

    Thanks,

    Cornelis
    Gouda - The Netherlands

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    In IE you may not have a single radio button in a group - if there is only one choice use a checkbox instead.

    Code:
    <form name = "myform">
    <select name = "mySelect" onchange = "showRadioButtons()">
    <option value = "0">Select ......</option>
    <option value = "1">One</option>
    <option value = "2">Two</option>
    </select>
    
    <div id = "rads" style="display:none">
    Check a radio button 1<input type = "radio" name = "rad1">2<input type = "radio" name = "rad1">
    </div>
    </form>
    
    <script type = "text/javascript">
    
    function showRadioButtons() {
    document.getElementById("rads").style.display="none"; // hide the radios
    var val = document.myform.mySelect.value;  
    if (val == 1) {
    document.getElementById("rads").style.display="block";  // show the radios if One selected
    var r = document.myform.rad1;
    for (var i =0; i<r.length; i++) {  // uncheck the radio previously selected
    r[i].checked = false;
    }
    }
    
    }
    
    </script>

    "I'm such a good lover because I practice a lot on my own." - Woody Allen
    Last edited by Philip M; 01-10-2012 at 09:09 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •