suttercain Posted May 20, 2009 Share Posted May 20, 2009 Hi everyone, I want to be able to show a div cotnainer based on the radio button selected. Example 3 options Yes, No, Don't Know. If yes is clicked a DIV is hown (I have this part working), but if Yes is clicked and the user changes it to No or Don't Know, I want to hide that DIV. This is the part I don't have working, the hide. Here is the code I am using that work with show, but not hide. $("#14a").click(function(){ // If checked if ($("#14a").is(":radio")) { //show the hidden div $("#extra").show("fast"); } else { //otherwise, hide it $("#extra").hide("fast"); } }); <input id="14a" type="radio" name="14" value="Yes" class="style1" /> Yes<br /> <input id="14b" type="radio" name="14" value="No" class="style1" /> No<br /> <input id="14c" type="radio" name="14" value="Don't Know" class="style1" /> Don't Know <div id="extra"> <br /><label>If yes, please specify.</label> <input id="email" type="text" class="style" /> </div> Any help would be much appreciated. Thank you. Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 20, 2009 Share Posted May 20, 2009 Well the reason is that you only listen to 'onclick' event of the radiobutton with the id '14a'. So clicking on another radio button wont fire your event. Try changing $("#14a").click(function() to $("#14a").change(function() (Guessing the name would simply be change in jquery) If that doesn't work make the function external and add a click event listener to 14b/14c as well, calling the new external function Quote Link to comment Share on other sites More sharing options...
suttercain Posted May 21, 2009 Author Share Posted May 21, 2009 Hi Axeia, I gave it a go and sadly it didn't work. The "show" still worked fine but when selecting a different radio button the DIV did not hide. Anyone else know of a possible solution? Thanks. Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 21, 2009 Share Posted May 21, 2009 Then use the alternative solution I posted. Did post two as I feared changes made my javascript wouldn't be detected. $("#14a").click(function(){ checkRb() }); $("#14b").click(function(){ checkRb() }); $("#14c").click(function(){ checkRb() }); function checkRb() { if ($("#14a").is(":radio")) { //show the hidden div $("#extra").show("fast"); } else { //otherwise, hide it $("#extra").hide("fast"); } } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 Give each input element some static class name like this - <input id="14a" type="radio" name="14" value="Yes" class="static_class style1" /> Yes<br /> <input id="14b" type="radio" name="14" value="No" class="static_class style1" /> No<br /> <input id="14c" type="radio" name="14" value="Don't Know" class="static_class style1" /> Don't Know <div id="extra"> <br /><label>If yes, please specify.</label> <input id="email" type="text" class="style" /> </div> <script type="text/javascript"> $(".static_class").click(function(){if($(this).val()==="Yes") $("#div").show("fast"); else $("#div").hide("fast");}); </script> Quote Link to comment Share on other sites More sharing options...
suttercain Posted June 4, 2009 Author Share Posted June 4, 2009 Works perfectly Ken, thank you so much!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.