orèli
orèli

Reputation: 89

Jquery each disabled

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

I like to have the following behavior:

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');
    };
 });
});

Upvotes: 0

Views: 3013

Answers (3)

efenacigiray
efenacigiray

Reputation: 340

Try changing your each function. $(selector) in each does not refer to current iteration element. Try this:

 $( ".choice" ).each(function(index, element) {
    if($(element).is(":disabled")) {
        $(element).addClass('disabled').remove('notdisabled');
    } else {
        $(element).addClass('notdisabled').remove('disabled');
    };
 });

Upvotes: 1

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

you need to check for the current input element, using this. Otherwise it will always checks for the first element with that class

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

});

Upvotes: 2

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

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/

Upvotes: 3

Related Questions