views:

135

answers:

6

Hi,

I have two radio buttons and a drop down box as you can see below. What I want to do is: 1. While no is checked, either hide, or grey out the drop down box, and 2. While yes is checked, show the drop down box.

Any pointers would be appreciated!

<td colspan="4">
<input name="discount" type="radio" id="Yes" value="Yes" />Yes
<input name="discount" type="radio" id="No" value="No" checked="checked" />No<br />  
<select class="purple" name="discountselection" id="discountselection">
<option value="1 Year" selected="selected">1 Year</option>
<option value="2 Years">2 Years</option>
<option value="3 Years">3 Years</option>
</select>                  
</td>
A: 

You need to set your select control to disabled and use a similar code to this one:

​$(function(){
    $('input:radio[name=discount]').one('change', function(){
        $('.purple').removeAttr('disabled');
    });
});​

See http://www.jsfiddle.net/A3BuQ/6/

Ref.: .one(), .removeAttr()

jAndy
I do have other radio buttons on the page. Will this affect those?
TaraWalsh
@TaraWalsh: yes it would. I adapted the code to make it more specific.
jAndy
Cool, I have placed this javascript code at the top of my HTML document like so: <script type='text/javascript'>$(function(){ $('input:radio[name=discount]').one('change', function(){ $('.purple').removeAttr('disabled'); });});​</script> Is this the correct way to use this code? Right now it doesn't seem to work.
TaraWalsh
This won't re-disable if they change back.
Robert
A: 
$('input:radio[name="discount"]').change(function() {
    if ($(this).val()=='Yes') {
        $('#discountselection').attr('disabled',true);
    } else
        $('#discountselection').removeAttr('disabled');
});​

http://jsfiddle.net/uSmVD/

Robert
A: 

You can hide it with jQuery:

$(document).ready(function(){     

$('#discountselection').hide();

        $('#No').click(function(){
            $('#discountselection').hide();
        });

        $('#Yes').click(function(){
            $('#discountselection').show();
        });
});

check it: http://www.jsfiddle.net/cFUsU/

UPDATE: added $(document).ready(); method to start set this code to action when the page is ready

d2burke
Hi, where should I insert this javascript? I'm a bit of a newbie to it.
TaraWalsh
You can put this inside a `<script type="text/javascript> //code here </script>` tag inside your `<head> </head>` tags in your HTMLSEE ABOVE: I made a few edits for you
d2burke
Yeah I've done that but it doesn't seem to work for me :-\ Is there anything else I need to do?
TaraWalsh
what does it do? can I see your code? I'm sure there is something simple missing :) Did you check it out in jsfiddle? If you have code I can look at, we'll get it going.
d2burke
+1  A: 

Can you please mark it as answer and vote accordingly ?

   <script type="text/javascript">
                   $("#Yes").click(function() {
                        $("#discountselection").attr("disabled", false);
                        //$("#discountselection").show(); //To Show the dropdown
                    });
                    $("#No").click(function() {
                        $("#discountselection").attr("disabled", true);
                        //$("#discountselection").hide();//To hide the dropdown
                    });
    </script>

Also, set the dropdown's display style, or disabled property in HTML based on your default radiobutton selected on page load.

 <select  name="discountselection" id="discountselection" disabled="disabled">
    <option value="1 Year" selected="selected">1 Year</option>
    <option value="2 Years">2 Years</option>
    <option value="3 Years">3 Years</option>
    </select>
Siva Gopal
This seems to do the job, just wondering where exactly do I put that javascript code. Fair new to JS!
TaraWalsh
Please see the modified jquery scriptlet above.
Siva Gopal
In response your 'calling for the question' (as in Roberts Rules of Order) Siva, I think you simply duplicated my response with your edit :) Outside of the 'disabled' code, which I don't think she's looking for necessarily. Great minds think alike I guess.
d2burke
A: 
​$(function(){
  $('input:radio').bind('change', function(){
     $('#discountselection').attr('disabled', !$("#yes").is(":checked"));
  });
});​
elektronikLexikon
A: 

Would this do it?

$('input:radio').bind('change', function(){
    $('select').attr('disabled', $(this).val() == "No");
});

Tested, works great. Good luck.

mkoistinen
this is trouble. not removing the `disabled` attribute could case some browser to still disable the element, even when it's value is 0. Actually it doesn't matter which value you set, as long as the attribute `disabled` is present, an element must get disabled (w3c).
jAndy