Created
June 22, 2012 21:09
-
-
Save nathancarnes/2975221 to your computer and use it in GitHub Desktop.
Use custom checkboxes instead of wonky browser ones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.fancyCheckboxes = function() { | |
return this.each(function() { | |
var input = this; | |
$(input).hide(); | |
var fancy_checkbox = | |
$('<span class="fancy_checkbox"/>') | |
.addClass( | |
$(input).attr('checked') ? 'checked' : '' | |
) | |
.on('click', function(){ | |
$(input).attr('checked') ? $(input).attr('checked', false) : $(input).attr('checked', 'checked'); | |
$(this).toggleClass('checked'); | |
$(input).trigger('click'); | |
}); | |
$(input).after(fancy_checkbox); | |
}); | |
}; | |
})(jQuery); |
Looks handy. I'm with amiel when it comes to setting input = $(this), and avoiding the repeated calls to the jquery wrapper. It cleans things up visually and also saves some cpu cycles. I've had no experience with setting checked attributes programatically, but it does seem like removing the attribute entirely makes the most sense here.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks pretty good to me.
Of course, I've always got thoughts. Here are a few:
checked
attribute. The method I've used with success was$(input).removeAttr('checked')
. However, this may have changed since I last dealt with that.var input = $(this)
and changing all instances of$(input)
to justinput
.