jing
jing

Reputation: 25

how to disable a drop down if nothing is selected from radio button

currently my drop down only disables (greys out) when radio button 'No' is selected. How do I disable drop down when nothing is selected aswell? colour and shade are the names of my 2 drop down menus.

  $("input[name^='attendance']").click(function() {                
            var id = this.name.replace('attendance', '');      
   $("#colour" + id + ", #shade" + id).prop("disabled", this.value == 'No');   

Upvotes: 1

Views: 381

Answers (3)

Shaun Bohannon
Shaun Bohannon

Reputation: 491

I think there is a more fundamental problem here as this code will never be executed in the way you expect. This is in the onClick event of the radio, meaning that it will always have a state (Yes/No).

You'll need to do this in two statements, something like this maybe?

$("input[name^='attendance']").click(function() {                
                var id = this.name.replace('attendance', '');      
       $("#colour" + id + ", #shade" + id).prop("disabled", this.value == 'No');
...
});
$("#colour" + id + ", #shade" + id).prop("disabled", !$(input[name^='attendance').val());

Upvotes: 1

DarkAjax
DarkAjax

Reputation: 16223

This should work for you:

 $("#colour" + id + ", #shade" + id).prop("disabled", this.value == 'No' || ! this.value );​​​​​​​

Upvotes: 0

Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14747

I imagine maybe something like this?

$().prop('disabled', !this.value || this.value == 'No');

Upvotes: 0

Related Questions