views:

46

answers:

1

I'm having trouble checking hidden checkboxes in IE. This is the base html:

<input id="groups_ids_1" name="group_ids[]" type="checkbox" value="1" />
<label for="groups_ids_1">Display</label>

This works fine, but if I then hide the checkboxes using either

$('input[type=checkbox]').hide();

or

$('input[type=checkbox]').css('visibility', 'hidden');

Clicking the label no longer checks the checkbox in IE. Of course it works fine in Firefox, Chrome and Safari.

A: 

You could try added an onclick to the label to get around the IE issues.

$('label').click(function() {
  $('#' + $(this).attr('for')).click();
});

If that does not work, try setting the attribute manually.

$('label').click(function() {
  var checkbox = $('#' + $(this).attr('for'));
  if (checkbox.is(':checked')) {
    checkbox.removeAttr('checked');
  } else {
    checkbox.attr('checked', 'checked');
  }
});
Nate Pinchot
I was hoping for some way to avoid this as I was using the .changed callback for my checkbox fields, and this felt a bit hacky. But after some experimenting it seems to be the only workaround.Thanks for your reaction :)
PeterD