AndiFaria
AndiFaria

Reputation: 27

JQuery won't check dynamically created checkboxes

I have some code with a dynamically created group of checkboxes. I need a specific check be checked when I change a dropdown to a certain value. Code is as follows:

$("#dropdown").change(function(){

    f = $(this).val(); //get dropdown value    

    if(f==9||f==10){ //if dropdown has one of those values
        for(i=0;i<$(".mycheck").length;i++){ //run through all checks   
            if($('.mycheck').eq(i).parent().text()=="TO CHECK"){ //get checkbox label; if match...              
                $('.mycheck').eq(i).prop('checked',true); //don't work
                $('.mycheck').eq(i).attr('checked','checked'); //don't work
                alert($('.mycheck').eq(i).val()) //WORKS!!!
                alert($('.mycheck').eq(i).attr('checked') //returns 'undefined'
            }        
        }

    }

});

Any help??

Upvotes: 1

Views: 377

Answers (1)

epascarello
epascarello

Reputation: 207511

With jQuery 1.6+, you should be using prop()

var checkbox = $('.mycheck').eq(i);
checkbox.prop('checked',true); //should work
alert(checkbox.prop('checked'));

JSFiddle

Upvotes: 1

Related Questions