1

I want to display a div if a certain radio button in a form is selected, but hide that div if any other radio button in the form is selected, using jquery.

This is the code I have that isn't working:

$("select").change(function(){
if($("#radio1").is(":selected")){
    $("#grid_9 omega").slideDown("slow");
} else { $("#grid_9 omega").slideUp("slow"); } 
});

where the id for the radio button I want to have display the div "gid_9 omega" is "radio1".

Thanks for your help!

1

2 Answers 2

2

I took the liberty of thinking that maybe omega was a class that you were referring to incorrectly.

First, there is no select here so that is not going to work for you. You need to test on all input[type=radio].

Next, radio button attribute for selected is actually checked=checked, so you need to test if the one you want to be checked is checked. If not, do nothing, if so, show your div.

A JSFiddle

Using a similar html structure to this below:

<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input type="radio" name="RadioGroup1" value="radio1" id="radio1" />
      Radio 1</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio2" id="radio2" />
      Radio 2</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio3" id="radio3" />
      Radio 3</label>
    <br />
  </p>
</form>
<div id="grid_9" class="omega" style="display:none">show me when Radio 1 is chosen​​​​​

your js would look like this:

$(document).ready(function() {
    $("input[type=radio]").on('click', function(){
        if ($('#radio1').is(':checked')){
            $("#grid_9.omega").slideDown("slow");
        } else { 
            $("#grid_9.omega").slideUp("slow");
        }
    });
});

Sign up to request clarification or add additional context in comments.

2 Comments

I implemented that code and it still doesn't work on the web page in question: intelligradeeducation.com/site_pro/tester.php
try enclosing it in a $(document).ready(function() {} );. I have change the js code to show you.
0

you should not use space between characters of an ID name, also a select element has nothing to do with radio buttons, try this:

$("#radio1").change(function(){
if($(this).is(":checked")) {
    $(".omega").slideDown("slow"); // note that `omega` has no "hash" or "dot" sign and this returns undefined
} else if ($('.omega').is(':visible')) { // makes sure that omega is visible
   $(".omega").slideUp("slow"); 
} 
});

5 Comments

I changed the name of the div to ensure there were no spaces in the ID name, but that code still doesn't work.
@user1484219 what is the markup? can you provide a jsfiddle?
Here's the URL of the web page: intelligradeeducation.com/site_pro/tester.php.
@user1484219 try updated answer, sounds that omega is a class.
a radio will never match :selected , no such property for radio, use :checked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.