http://jsfiddle.net/dmitryfil/4pYaW/1/
- Browser - Chrome
- I have a series of checkboxes where one is checked by default.
- I have a select with a 2 options (where value of an option is linked to checkbox)
- When I try to check checkboxes, based on a select (so 2 should be selected), only 1 checkbox gets selected, and that's what I'm trying to figure out, why?
HTML:
<input type="checkbox" name="item-a" checked="checked" />
<input type="checkbox" name="item-b" />
<input type="checkbox" name="item-c" />
<hr />
<select multiple>
<option value="a">a</option>
<option value="b">b</option>
</select>
JS:
$('input[type=checkbox]:checked').attr('checked', false);
//$('input[type=checkbox]:checked).removeAttr('checked');
$('select option').each(function(i, el){
var val = $(this).attr('value');
$('input[name=item-'+val+']').attr('checked', 'checked');
//$('input[name=item-'+val+']').attr('checked', true);
});
Notes:
- if checkbox is not checked by default, it works fine.
- when I unchecking with: attr('checked', false) and removeAttr('checked') - those produce different results.
Thanks a lot for any suggestions.