Created
June 20, 2013 16:50
-
-
Save Wilfred/5824453 to your computer and use it in GitHub Desktop.
Styling <input type="checkbox"> using <span>s and updating the input accordingly according to events
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
| // replace real checkboxes with fake ones using spans that work the same | |
| $('input[type=checkbox]').each(function() { | |
| var $input = $(this); | |
| $input.hide(); | |
| var $fakeCheckbox = $('<div class="fake-checkbox"><span class="icon-TICK"></span></div>'); | |
| $input.after($fakeCheckbox); | |
| // when our fake checkbox is clicked | |
| $fakeCheckbox.click(function() { | |
| // toggle the visible tick | |
| $fakeCheckbox.children().toggle(); | |
| // toggle the hidden form element | |
| $input.prop("checked", !$input.prop("checked")); | |
| }); | |
| // if the checkbox is unticked on page load, set our fake checkbox accordingly | |
| if (!$input.prop('checked')) { | |
| $fakeCheckbox.children().hide(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment