surfbird0713
surfbird0713

Reputation: 1219

show hide list items with radio button selection

I would like to show/hide list items depending on the radio button selected. i have 3 buttons, each should reveal a different set of fields (inside list items). for some reason, only the last one works. what am i doing wrong? i followed the suggestions here and mine matches from what i can tell.

jsfiddle posted here: http://jsfiddle.net/Vm2aA/

here is my jQuery:

        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
        <script type="text/javascript">

                    $(document).ready(function() {
                        $( "#datepicker" ).datepicker({ minDate: -1, maxDate: "+24D" });
                        $('#pancettaForm').change(function() {
                            if ($('#ship-address').prop('checked')) {
                                $('#address').show();
                            } else {
                                $('#address').hide();
                            }
                            if ($('#ship-date').prop('checked')) {
                                $('#new-ship-date').show();
                            } else {
                                $('#new-ship-date').hide();
                            }    
                            if ($('#ship-both').prop('checked')) {
                                $('#address, #new-ship-date').show();
                            } else {
                                $('#address, #new-ship-date').hide();
                            }        
                        });
                    });
        </script> 

html

    <form name="pancettaForm" method="post" action="http://lizlantz.com/lcform.php" id="pancettaForm">
            <input type="hidden" value="Pancetta Order Update" name="subject"> 
            <input type="hidden" value="cookware/partners_10151_-1_20002" name="redirect">
            <ul>
                <li>
                <label for="update-ship">I'd like to:</label> 
                    <input id="ship-address" name="update-ship" type="radio" value="update-ship-address"/> Have pancetta shipped to a different address than my skillet<br />
                    <input id="ship-date" name="update-ship" type="radio" value="update-ship-date" /> Have pancetta shipped sooner than June 14, 2013 <br />
                    <input id="ship-both" name="update-ship" type="radio" value="update-both" /> Make changes to both the shipping address and shipping date
                </li>
                <li>                
                <label for="order-number"><em>*</em>Order Number (available in order confirmation email):</label> 
                    <input type="text" name="order-number">
                </li>             
                <li>                
                <label for="full-name"><em>*</em>Recipient Full Name:</label> 
                    <input type="text" name="full-name">
                </li>   
                <li id="address" style="display: none;">
                    <label for="address">
                        <em>*</em>Address
                    </label> 
                    <input type="text" name="address">
                    <label for="address2">
                        Address Line 2
                    </label> 
                    <input type="text" name="address2">
                </li>
                <li id="address2" style="display: none;">
                    <label for="city">
                        <em>*</em>City
                    </label> 
                    <input type="text" name="city">
                    <label for="state">
                        <em>*</em>State
                    </label> 
                    <select name="state">
                        <option value="AL">Alabama</option>
                        <option value="AK">Alaska</option>
                        <option value="AZ">Arizona</option>
                    </select>
                    <label for="zip">
                        <em>*</em>Zip Code
                    </label> 
                    <input type="text" name="zip">
                </li>
                <li id="new-ship-date"  style="display: none;">
                    <em>*</em><label for="update-ship">New Ship Date:</label>
                    <input type="text" id="datepicker" />
                </li>            
                <li>
                    <label for="phone">
                        <em>*</em>Phone (for delivery quetsions)
                    </label> 
                    <input type="text" name="phone">
                </li>               
            </ul>
                   <input type="submit" id="button" name="submit" class="green">

          </form>

Upvotes: 0

Views: 2387

Answers (2)

Ejaz
Ejaz

Reputation: 8872

I would suggest that you follow the "Hide all then show a particular list" approach if you want to target only radio buttons instead of selectively hiding and displaying items. It'll let you get rid of all of the else{hide...} clauses and will make your code more maintainable. Try the following code

        $('#pancettaForm').change(function () {

           //hide all optional li's
           $('#address,#address2,#new-ship-date').hide();

           //and then show which one is necessary
           if ($('#ship-address').prop('checked')) {
              $('#address').show();
           }

           else if ($('#ship-date').prop('checked')) {
              $('#new-ship-date').show();
           }

           else if ($('#ship-both').prop('checked')) {
              $('#address, #new-ship-date').show();
           }
        });

Also notice the else if instead of if. The code also works with else if because only one radio button can be checked at a given instance. And notice that I've changed the selector to work only with your radio buttons

Update

Above code does its job for now but you must note that $('pancettaForm').change(...) means it will run if the form is changed in any way, e.g., if a textbox value is updated. It will only result in some extra processing for now. To remove this potential problem, you can replace the above code with following code

       $('#pancettaForm').change(function (ev) {

           //execute this block only if one of the radio button from "update-ship" group has changed
           if (ev.target.name == 'update-ship') {

              //hide all optional li's
              $('#address,#address2,#new-ship-date').hide();

              //and then show which one is necessary
              if ($('#ship-address').prop('checked')) {
                 $('#address').show();
              }

              else if ($('#ship-date').prop('checked')) {
                 $('#new-ship-date').show();
              }

              else if ($('#ship-both').prop('checked')) {
                 $('#address, #new-ship-date').show();
              }
           }
        }); 

Upvotes: 1

dwaddell
dwaddell

Reputation: 1496

It is because you are hiding the element in the last if statement.

 $('#address, #new-ship-date').hide();

This is returning false (so right after one of the elements is shown it is hidden again):

if ($('#ship-both').prop('checked'))

Upvotes: 0

Related Questions