Skip to content

Instantly share code, notes, and snippets.

@kujon
Created June 5, 2012 13:34
Show Gist options
  • Save kujon/2875028 to your computer and use it in GitHub Desktop.
Save kujon/2875028 to your computer and use it in GitHub Desktop.
JavaScript: Radio button and checkbox replacement
/**
* Radio button and checkbox replacement
* Author: Jakub Korzeniowski
* Agency: Softhis
* http://www.softhis.com
*/
(function($) {
$.fn.checked = function() {
return this.each(function() {
var $org = $(this).filter(':radio, :checkbox'),
$repl = $('<span />', {
class: $org.attr('type')
});
if($org.is(':checked')) {
$repl.addClass('checked');
}
$org.css({
visibility: 'hidden',
position: 'absolute',
'z-index': -1
}).after($repl.bind('click', function(e) {
if($repl.hasClass('checked')) {
$repl.removeClass('checked');
$org.attr('checked', false);
} else {
$repl.addClass('checked');
$org.attr('checked', true);
}
}));
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment