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.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Cameron
  • 27,963
  • 100
  • 281
  • 483

5 Answers5

16

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
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 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 Sep 24 '10 at 21:51
  • 1
    @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 Sep 24 '10 at 22:26
  • @Nick: a very elegant solution, I'll be adding it to my toolbox! (If you swing that way, you can make this even terser: remove the final line and rewrite the third line as `}).filter(':checked').change();`.) – Jordan Gray Oct 13 '11 at 11:07
  • I'm just learning jQuery (and JS). In my app, it's possible that neither radio button is initially checked, in which case the last line doesn't select anything to trigger. How do I handle that case? – Bob.at.Indigo.Health Mar 11 '13 at 05:01
  • Never mind... I just realized that I missed the "style=display:none", which causes the second set of radio buttons to be initially hidden. – Bob.at.Indigo.Health Mar 11 '13 at 07:20
3

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
  • 31,561
  • 18
  • 73
  • 106
1

Ref Nick Craver - Nice solution although it is more felxaible as below

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

By leaving the toggle as a wild card (undefined whichever terminology you prefer) I found it worked more efficiently. Nice though, thanks :)

Peter
  • 26
  • 1
  • This doesn't work more efficiently because it doesn't fit the "work" part under many conditions. Browser caching, clicking before `document.ready`, *or* going back on the same page can leave your toggle in the *opposite* state. Any of those three break the approach in this answer...that's why I have an explicit check for the value. – Nick Craver Oct 25 '11 at 20:22
0

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
  • 8,714
  • 4
  • 46
  • 69
0
$(document).ready(function() {
    $("input[name=solidworks]").change(function() {
        if ($this).val() == "Yes") {
            $("#boxReseller").slideDown('fast');
        }
        else {
            $("#boxReseller").hide('fast');
        }
    })
})
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
Liam Bailey
  • 5,879
  • 3
  • 34
  • 46