Skip to content

Instantly share code, notes, and snippets.

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