Solved

Need help with radio group buttons show/hide div layer...

Posted on 2011-09-13
3
433 Views
Last Modified: 2012-05-12
Hello,

I have four radio buttons in one group. Basically I need help with hiding div layer "my_box" initially and if the user selects radio buttons 2,3 or 4 then show the my_box div. If the user selects the 1st radio button then hide the "my_box" div.

I got it working initially but can't seem to get back to where it was working again plus I want to see if somebody else has a different way of coding it.

Thank you,

Brian
<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_0" />
      Radio</label>
1    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_1" />
      Radio 2</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_2" />
      Radio 3</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_3" />
      Radio</label>
4    <br />
  </p>
</form>

<div id="my_box" style="display: none;">
	Stuff...
</div>

Open in new window

0
Comment
Question by:brihol44
3 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 125 total points
Comment Utility

<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input type="radio" name="RadioGroup1" value="radio1" id="RadioGroup1_0" onclick="showHide(this)" />
      Radio</label>
1    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio2" id="RadioGroup1_1" onclick="showHide(this)" />
      Radio 2</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio3" id="RadioGroup1_2" onclick="showHide(this)" />
      Radio 3</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio4" id="RadioGroup1_3" onclick="showHide(this)" />
      Radio</label>
4    <br />
  </p>
</form>

<div id="my_box" style="display: none;">
	Stuff...
</div>
<script type="text/javascript" language="javascript">
function showHide(obj) {
    switch (obj.value) {
        case "radio2" : case "radio3" : case "radio4" :
         document.getElementById('my_box').style.display = 'block';
         break;
        default:
         document.getElementById('my_box').style.display = 'none';
    }
}

</script>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
With jQuery : http://jsfiddle.net/cSHfm/

$(document).ready(function() {
    $("input[name='RadioGroup1']").change(function() {
        if( $(this).attr("id") == "RadioGroup1_0" && $(this).is(":checked") ) {
            $("#my_box").hide();
        }
        else {
            $("#my_box").show();
        }
    })
})

Open in new window

0
 

Author Closing Comment

by:brihol44
Comment Utility
Thx!

Brian
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now