views:

308

answers:

3

The Code:

var lstInstanceIds = getData.lstInstanceIds.split(',');
    for(var i=0; i<lstInstanceIds.length; i++) {
        var value = lstInstanceIds[i];
        $('input[value=' + value + ']').attr('checked','checked');
    }

So all i'm doing is looping a list and setting the attribute checked as checked where the values meet.

This works fine in Chrome, Firefox, IE7/8, Safari. But not in IE6...

+4  A: 

Use attr('checked', true) instead.

Andy E
+1, jQuery handles the browser inconsistencies here if you pass true/false.
Nick Craver
Tried that but still no change. :\
Jonathon Joyce
A: 

I was able to setup a basic example based solely on what you mentioned here (so hopefully I'm not too far off), and it worked testing in IETester with a 6 instance.

<input type="checkbox" id="chk1" value="chk1" />
<input type="checkbox" id="chk2" value="chk2" />
<input type="checkbox" id="chk3" value="chk3" />
<input type="checkbox" id="chk4" value="chk4" />
<input type="checkbox" id="chk5" value="chk5" />

var ids = "chk1,chk3,chk5";
var lstInstanceIds = ids.split(',');
for (var i=0; i<lstInstanceIds.length; i++) {
    var value = lstInstanceIds[i];
    $('input[value=' + value + ']').attr('checked','checked');
}
Adam Presley
A: 

I've had a similar problem before. To fix it, I used code similar to this:

var lstInstanceIds = getData.lstInstanceIds.split(',');
for(var i=0; i<lstInstanceIds.length; i++) {
    var value = lstInstanceIds[i];
    var checkbox = $('input[value=' + value + ']').get(0);
    if(checkbox.checked == false)
        checkbox.click();
}

This checks whether the checkbox is checked. If not, it clicks it, thereby checking it. Should support all browsers!

Aaron