Skip to content

Instantly share code, notes, and snippets.

@ahsquared
Last active December 22, 2015 11:39
Show Gist options
  • Save ahsquared/6467488 to your computer and use it in GitHub Desktop.
Save ahsquared/6467488 to your computer and use it in GitHub Desktop.
select checkboxes to style: $(':checkbox').styleCheckboxes();
// select checkboxes to style: $(':checkbox').styleCheckboxes();
$.fn.styleCheckboxes = function() {
$(this).each(function(index) {
if (!$(this).prev("a").hasClass('check-styled')) {
$(this).before("<a href='#check_" + index + "' class='check-styled' id='check_" + index + "'></a>").css({
position: 'absolute',
left: -1,
top: 3,
zIndex: -1,
visibility: "hidden"
});
if ($(this).prop('checked'))
$(this).prev().addClass('checked');
$(this).change(function() {
if ($(this).prop('checked')) {
$(this).prev().addClass('checked');
} else {
$(this).prev().removeClass('checked');
}
});
}
});
// function to 'check' the styled ones and their matching checkboxes
$(".check-styled").on('click', function() {
$(this).next("input[type=checkbox]").trigger("click").trigger('change');
return false;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment