0

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??

Uyghur Lives Matter
  • 17,261
  • 40
  • 105
  • 135
AndiFaria
  • 17
  • 3

1 Answers1

1

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

epascarello
  • 195,511
  • 20
  • 184
  • 225
  • Thanks for answering, however, .prop doesn't work for some reason. I'm using JQuery 1.9.1, which works fine with everything else... – AndiFaria Feb 12 '14 at 17:28