Search code examples
jqueryeachdisabled-input

Jquery each disabled


with this code : http://jsfiddle.net/oreli/7wdq6ktn/

I like to have the following behavior:

  • When input is "disabled": the label is red
  • When input is not "disabled": label is green (or vice versa, no matter :) )

but I still have all red or green.

What is my mistake?

thank you

$(document).ready(function () {
 $( ".choice" ).each(function() {
    if($(".orderChoice").is(":disabled")) {
        $(".choice").addClass('disabled').remove('notdisabled');
    } else {
        $(".choice").addClass('notdisabled').remove('disabled');
    };
 });
});

Solution

  • You are selecting all the .choice and applying the class. Use context:

    $(document).ready(function () {
    
        $( ".choice" ).each(function() {
            if($(this).find("input").is(":disabled")) {
                $(this).addClass('disabled').remove('notdisabled');
            } else {
                $(this).addClass('notdisabled').remove('disabled');
            };
        });
    
    });
    

    Fiddle: http://jsfiddle.net/7wdq6ktn/2/