Skip to content

Instantly share code, notes, and snippets.

@brainded
Last active January 4, 2016 03:49
Show Gist options
  • Save brainded/8564798 to your computer and use it in GitHub Desktop.
Save brainded/8564798 to your computer and use it in GitHub Desktop.
Custom checkbox that works with all browsers, web testing frameworks as well as accessibility tools like Chrome Vox.
.custom-checkbox input[type="checkbox"] {
display: inline-block;
width: 20px;
height: 20px;
/*hide the actual checkbox, but dont do a display none because that breaks a ton of stuff like testing frameworks*/
opacity: 0.01;
filter: alpha(opacity=0.01);
cursor: pointer;
}
.custom-checkbox {
display: inline-block;
width: 20px;
height: 20px;
vertical-align: middle;
background: url(../../images/checkbox.png) 0 20px;
cursor: pointer;
}
.custom-checked {
background: url(../../images/checkbox.png) 0 0px;
cursor: pointer;
}
/*retina icons*/
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
.custom-checkbox {
background: url(../../images/checkbox@2x.png) no-repeat;
background-size: 20px 20px;
}
}
var controls = controls || {};
controls.checkbox = (function () {
//safari seems to have issues updating the look and feel when using jquery to modify css classes
var _redraw = function () {
$('body').addClass('redraw').removeClass('redraw');
};
(function initialize() {
//find all custom checkboxes that are already checked and add the checked class
$('.custom-checkbox input:checked').toggleClass('custom-checked');
_redraw();
//on change of the checkbox, toggle the checked class
$(document).on('change', '.custom-checkbox input[type="checkbox"]', function () {
$(this).parent().toggleClass('custom-checked');
// safari mac repaint fix
_redraw();
});
}); // initialization immediate function call
});
<div class="custom-checkbox">
<input type="checkbox" />
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment