jquery.customforms.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * jQuery Custom Forms Plugin 1.0
  3. * www.ZURB.com
  4. * Copyright 2010, ZURB
  5. * Free to use under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. jQuery(document).ready(function ($) {
  9. function appendCustomMarkup(type) {
  10. $('form.custom input:' + type).each(function () {
  11. var $this = $(this).hide(),
  12. $span = $this.next('span.custom.' + type);
  13. if ($span.length === 0) {
  14. $span = $('<span class="custom ' + type + '"></span>').insertAfter($this);
  15. }
  16. $span.toggleClass('checked', $this.is(':checked'));
  17. });
  18. }
  19. appendCustomMarkup('checkbox');
  20. appendCustomMarkup('radio');
  21. function appendCustomSelect(sel) {
  22. var $this = $(sel),
  23. $customSelect = $this.next('div.custom.dropdown'),
  24. $options = $this.find('option'),
  25. maxWidth = 0,
  26. $li;
  27. if ($customSelect.length === 0) {
  28. $customSelect = $('<div class="custom dropdown"><a href="#" class="selector"></a><ul></ul></div>"');
  29. $options.each(function () {
  30. $li = $('<li>' + $(this).html() + '</li>');
  31. $customSelect.find('ul').append($li);
  32. });
  33. $customSelect.prepend('<a href="#" class="current">' + $options.first().html() + '</a>');
  34. $this.after($customSelect);
  35. $this.hide();
  36. } else {
  37. // refresh the ul with options from the select in case the supplied markup doesn't match
  38. $customSelect.find('ul').html('');
  39. $options.each(function () {
  40. $li = $('<li>' + $(this).html() + '</li>');
  41. $customSelect.find('ul').append($li);
  42. });
  43. }
  44. $options.each(function (index) {
  45. if (this.selected) {
  46. $customSelect.find('li').eq(index).addClass('selected');
  47. $customSelect.find('.current').html($(this).html());
  48. }
  49. });
  50. $customSelect.find('li').each(function () {
  51. $customSelect.addClass('open');
  52. if ($(this).outerWidth() > maxWidth) {
  53. maxWidth = $(this).outerWidth();
  54. }
  55. $customSelect.removeClass('open');
  56. });
  57. $customSelect.css('width', maxWidth + 18 + 'px');
  58. $customSelect.find('ul').css('width', maxWidth + 16 + 'px');
  59. }
  60. $('form.custom select').each(function () {
  61. appendCustomSelect(this);
  62. });
  63. });
  64. (function ($) {
  65. function refreshCustomSelect($select) {
  66. var maxWidth = 0,
  67. $customSelect = $select.next();
  68. $options = $select.find('option');
  69. $customSelect.find('ul').html('');
  70. $options.each(function () {
  71. $li = $('<li>' + $(this).html() + '</li>');
  72. $customSelect.find('ul').append($li);
  73. });
  74. // re-populate
  75. $options.each(function (index) {
  76. if (this.selected) {
  77. $customSelect.find('li').eq(index).addClass('selected');
  78. $customSelect.find('.current').html($(this).html());
  79. }
  80. });
  81. // fix width
  82. $customSelect.removeAttr('style')
  83. .find('ul').removeAttr('style');
  84. $customSelect.find('li').each(function () {
  85. $customSelect.addClass('open');
  86. if ($(this).outerWidth() > maxWidth) {
  87. maxWidth = $(this).outerWidth();
  88. }
  89. $customSelect.removeClass('open');
  90. });
  91. $customSelect.css('width', maxWidth + 18 + 'px');
  92. $customSelect.find('ul').css('width', maxWidth + 16 + 'px');
  93. }
  94. function toggleCheckbox($element) {
  95. var $input = $element.prev(),
  96. input = $input[0];
  97. input.checked = ((input.checked) ? false : true);
  98. $element.toggleClass('checked');
  99. $input.trigger('change');
  100. }
  101. function toggleRadio($element) {
  102. var $input = $element.prev(),
  103. input = $input[0];
  104. $('input:radio[name="' + $input.attr('name') + '"]').each(function () {
  105. $(this).next().removeClass('checked');
  106. });
  107. input.checked = ((input.checked) ? false : true);
  108. $element.toggleClass('checked');
  109. $input.trigger('change');
  110. }
  111. $('form.custom span.custom.checkbox').live('click', function (event) {
  112. event.preventDefault();
  113. event.stopPropagation();
  114. toggleCheckbox($(this));
  115. });
  116. $('form.custom span.custom.radio').live('click', function (event) {
  117. event.preventDefault();
  118. event.stopPropagation();
  119. toggleRadio($(this));
  120. });
  121. $('form.custom select').live('change', function (event) {
  122. refreshCustomSelect($(this));
  123. });
  124. $('form.custom label').live('click', function (event) {
  125. var $associatedElement = $('#' + $(this).attr('for')),
  126. $customCheckbox,
  127. $customRadio;
  128. if ($associatedElement.length !== 0) {
  129. if ($associatedElement.attr('type') === 'checkbox') {
  130. event.preventDefault();
  131. $customCheckbox = $(this).find('span.custom.checkbox');
  132. toggleCheckbox($customCheckbox);
  133. } else if ($associatedElement.attr('type') === 'radio') {
  134. event.preventDefault();
  135. $customRadio = $(this).find('span.custom.radio');
  136. toggleRadio($customRadio);
  137. }
  138. }
  139. });
  140. $('form.custom div.custom.dropdown a.current, form.custom div.custom.dropdown a.selector').live('click', function (event) {
  141. var $this = $(this),
  142. $dropdown = $this.closest('div.custom.dropdown');
  143. event.preventDefault();
  144. $dropdown.toggleClass('open');
  145. if ($dropdown.hasClass('open')) {
  146. $(document).bind('click.customdropdown', function (event) {
  147. $dropdown.removeClass('open');
  148. $(document).unbind('.customdropdown');
  149. });
  150. } else {
  151. $(document).unbind('.customdropdown');
  152. }
  153. });
  154. $('form.custom div.custom.dropdown li').live('click', function (event) {
  155. var $this = $(this),
  156. $customDropdown = $this.closest('div.custom.dropdown'),
  157. $select = $customDropdown.prev(),
  158. selectedIndex = 0;
  159. event.preventDefault();
  160. event.stopPropagation();
  161. $this
  162. .closest('ul')
  163. .find('li')
  164. .removeClass('selected');
  165. $this.addClass('selected');
  166. $customDropdown
  167. .removeClass('open')
  168. .find('a.current')
  169. .html($this.html());
  170. $this.closest('ul').find('li').each(function (index) {
  171. if ($this[0] == this) {
  172. selectedIndex = index;
  173. }
  174. });
  175. $select[0].selectedIndex = selectedIndex;
  176. $select.trigger('change');
  177. });
  178. })(jQuery);