views:

61

answers:

4

I have the following code:

    <fieldset>
        <legend>Do you currently have SolidWorks</legend>

        <ul>
            <li><label for=""><input type="radio" name="solidworks" value="Yes" id="rdYes" /> Yes</label></li>
            <li><label for=""><input type="radio" name="solidworks" value="No" id="rdNo" /> No</label></li>
        </ul>
    </fieldset>

    <fieldset id="boxReseller" style="display:none;">
        <legend>Who is your SolidWorks reseller?</legend>
        <ul>
            <li><label for=""><input type="radio" name="reseller" value="Cad Connect" /> Cad Connect</label></li>
            <li><label for=""><input type="radio" name="reseller" value="Cadtek" /> Cadtek</label></li>
            <li><label for=""><input type="radio" name="reseller" value="CCSL" /> CCSL</label></li>
            <li><label for=""><input type="radio" name="reseller" value="Innova" /> Innova</label></li>
            <li><label for=""><input type="radio" name="reseller" value="NT CAD/CAM" /> NT CAD/CAM</label></li>
            <li><label for=""><input type="radio" name="reseller" value="Solid Engineer" /> Solid Engineer</label></li>
            <li><label for=""><input type="radio" name="reseller" value="Solid Solutions Ireland" /> Solid Solutions Ireland</label></li>
            <li><label for=""><input type="radio" name="reseller" value="Solid Solutions Management" /> Solid Solutions Management</label></li>
            <li><label for=""><input type="radio" name="reseller" value="TMS Scotland" /> TMS Scotland</label></li>
        </ul>

    </fieldset>

What I want to do is hide the second fieldset by default and if a person chooses Yes then the box will appear, and if they choose No or Yes is not selected then the box will hide again.

Can anyone help? Thanks.

A: 

Somthing like (Sorry if there are typos, my coffee isn't made yet).:

 <fieldset id="fs">
        <legend>Do you currently have SolidWorks</legend>

        <ul>
            <li><label for=""><input type="radio" name="solidworks" value="Yes" id="rdYes" /> Yes</label></li>
            <li><label for=""><input type="radio" name="solidworks" value="No" id="rdNo" /> No</label></li>
        </ul>
    </fieldset>

There are ways of making this look better, but my coffee is not made. will edit once I get my elixer.

<script>attr('checked','checked')
     $("#fs:checkbox").click(function(){
              if($("#rdYes:checked").attr('checked','checked'))
              {
                 $("#boxReseller").css('display', 'block'); })
              }          
     });
</script>
Ryan Ternier
Unneccesary code: when you click the No radio it will automatically decheck the Yes
Liam Bailey
A: 
$(document).ready(function() {
    $("input[name=solidworks]").change(function() {
        if ($this).val() == "Yes") {
            $("#boxReseller").slideDown('fast');
        }
        else {
            $("#boxReseller").hide('fast');
        }
    })
})
Liam Bailey
+4  A: 

You could do this:

$("input[name='solidworks']").change(function() {
  $("#boxReseller").toggle(this.value == "Yes");
});​​​​​​
$("input[name='solidworks']:checked").change(); //trigger correct state onload

You can give it a try with the markup in the question here, and try the pre-checked "Yes" version here.

Nick Craver
Better answer than mine :)
Ryan Ternier
This works great, can you explain how it works please as it seems a lot more confusing than the standard if and else statements others have used, but works better.
Cameron
@Cameron - Sure :) The `.toggle(bool)` takes a boolean, whether to call `.show()` (if true) or `.hide()` (if false), so we're just checking that the radio button that just changed was the "Yes" one or not. The last line gets the selected one and runs the handler on it...again if its value is "Yes" it shows, otherwise it hides, make sense?
Nick Craver
A: 

Demo

http://jsfiddle.net/Wpt3Y/

jQuery(function(){
        jQuery("input[name=solidworks]").change(function(){          


            if ($(this).val() == "Yes") {
            jQuery("#boxReseller").slideDown()
            }
            else {
            jQuery("#boxReseller").slideUp();
            }                                                            
       });
});

Praveen Prasad
What if a user refreshes the page and the Yes is already checked? The box is hidden.
Cameron
in that case you have to check the value of radio at DOM ready and hide/show your BOX
Praveen Prasad