custom_checkbox_and_radio.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Custom checkbox and radios
  2. function setupLabel() {
  3. // Checkbox
  4. var checkBox = ".checkbox";
  5. var checkBoxInput = checkBox + " input[type='checkbox']";
  6. var checkBoxChecked = "checked";
  7. var checkBoxDisabled = "disabled";
  8. // Radio
  9. var radio = ".radio";
  10. var radioInput = radio + " input[type='radio']";
  11. var radioOn = "checked";
  12. var radioDisabled = "disabled";
  13. // Checkboxes
  14. if ($(checkBoxInput).length) {
  15. $(checkBox).each(function(){
  16. $(this).removeClass(checkBoxChecked);
  17. });
  18. $(checkBoxInput + ":checked").each(function(){
  19. $(this).parent(checkBox).addClass(checkBoxChecked);
  20. });
  21. $(checkBoxInput + ":disabled").each(function(){
  22. $(this).parent(checkBox).addClass(checkBoxDisabled);
  23. });
  24. };
  25. // Radios
  26. if ($(radioInput).length) {
  27. $(radio).each(function(){
  28. $(this).removeClass(radioOn);
  29. });
  30. $(radioInput + ":checked").each(function(){
  31. $(this).parent(radio).addClass(radioOn);
  32. });
  33. $(radioInput + ":disabled").each(function(){
  34. $(this).parent(radio).addClass(radioDisabled);
  35. });
  36. };
  37. };
  38. $(document).ready(function(){
  39. $("html").addClass("has-js");
  40. // First let's prepend icons (needed for effects)
  41. $(".checkbox, .radio").prepend("<span class='icon'></span><span class='icon-to-fade'></span>");
  42. $(".checkbox, .radio").click(function(){
  43. setupLabel();
  44. });
  45. setupLabel();
  46. });