Show Hide multiple div tags from multiple radio buttons

Hi,
I have the following jquery code snip that shows or hides a DIV based on a radio button selection which is great for 2 radio buttons. Problem is I now have 10 radio buttons, while I could write the if / else for each radio button and div it seems like a lot of code to do something simple. Is there an easy way to accomplish this for 10 radio buttons / divs.

<script type="text/javascript">
        $(document).ready(function () {

            $("input[name$='choosecanvassize']").click(function () {

                var radio_value = $(this).val();

                if (radio_value == 'rect10x8') {
                    $("#yesbox").show("slow");
                    $("#nobox").hide();
                }
                else if (radio_value == 'rect14x8') {
                    $("#nobox").show("slow");
                    $("#yesbox").hide();
                }
            });

            $("#yesbox").hide();
            $("#nobox").hide();
        });
</script>

Open in new window


Thanks
nl
LVL 2
nedloganAsked:
Who is Participating?
 
Roman GhermanSenior Software EngineerCommented:
the best you can do here I think is:

 
 var radio_value = $(this).val();

                 $("#nobox, #yesbox").hide();

                if (radio_value == 'rect10x8') {
                    $("#yesbox").show("slow");
                }
                else if (radio_value == 'rect14x8') {
                    $("#nobox").show("slow");
                }
                else if (radio_value == 'anotherrbtn') {
                    $("#anotherrbtnBOX").show("slow");
                }

Open in new window


THis is all that I can think about.
Otherwise if you have an array that would map radio_value with the div id then it would be easier
0
 
Roman GhermanSenior Software EngineerCommented:
I see depending on the radiobutton you perform different actions. what action are you going to perform for other radiobuttons?
0
 
nedloganAuthor Commented:
Show other divs. There are 10 radio buttons and depending on which one is clicked the user will see 1 of 10 divs containing some content.
nl  
0
 
nedloganAuthor Commented:
Hi,
Ok, well that seems to work, I can't be bothered with arrays at the moment. Thanks for your help.
Regards,
NL
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.