Solved

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

Posted on 2011-09-13
3
436 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
ID: 36530555

<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
ID: 36530734
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
ID: 36530773
Thx!

Brian
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

770 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