custom-form-elements.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. CUSTOM FORM ELEMENTS
  3. Created by Ryan Fait
  4. www.ryanfait.com
  5. The only things you may need to change in this file are the following
  6. variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)
  7. The numbers you set for checkboxHeight and radioHeight should be one quarter
  8. of the total height of the image want to use for checkboxes and radio
  9. buttons. Both images should contain the four stages of both inputs stacked
  10. on top of each other in this order: unchecked, unchecked-clicked, checked,
  11. checked-clicked.
  12. You may need to adjust your images a bit if there is a slight vertical
  13. movement during the different stages of the button activation.
  14. The value of selectWidth should be the width of your select list image.
  15. Visit http://ryanfait.com/ for more information.
  16. */
  17. var checkboxHeight = "25";
  18. var radioHeight = "25";
  19. var selectWidth = "210";
  20. /* No need to change anything after this */
  21. document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');
  22. var Custom = {
  23. init: function() {
  24. var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
  25. for(a = 0; a < inputs.length; a++) {
  26. if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && $(inputs[a]).hasClass("styled") && !$(inputs[a]).hasClass("style-applied")) {
  27. $(inputs[a]).addClass('style-applied');
  28. span[a] = document.createElement("span");
  29. span[a].className = inputs[a].type;
  30. if ($(inputs[a]).attr('class').indexOf('do_action_toggle_suspend') != -1) {
  31. span[a].className += ' do_action_toggle_suspend'; // save toggle functionality
  32. }
  33. if ($(inputs[a]).attr('class').indexOf('do_action_toggle_batch_selector') != -1) {
  34. span[a].className += ' do_action_toggle_batch_selector'; // save toggle functionality
  35. }
  36. if(inputs[a].checked == true) {
  37. if(inputs[a].type == "checkbox") {
  38. position = "0 -" + (checkboxHeight*2) + "px";
  39. span[a].style.backgroundPosition = position;
  40. } else {
  41. position = "0 -" + (radioHeight*2) + "px";
  42. span[a].style.backgroundPosition = position;
  43. }
  44. }
  45. inputs[a].parentNode.insertBefore(span[a], inputs[a]);
  46. inputs[a].onchange = Custom.clear;
  47. if(!inputs[a].getAttribute("disabled")) {
  48. span[a].onmousedown = Custom.pushed;
  49. span[a].onmouseup = Custom.check;
  50. } else {
  51. span[a].className = span[a].className += " disabled";
  52. }
  53. }
  54. }
  55. inputs = document.getElementsByTagName("select");
  56. try {
  57. for(a = 0; a < inputs.length; a++) {
  58. if($(inputs[a]).hasClass("styled")) {
  59. option = inputs[a].getElementsByTagName("option");
  60. active = option[0].childNodes[0].nodeValue;
  61. textnode = document.createTextNode(active);
  62. for(b = 0; b < option.length; b++) {
  63. if(option[b].selected == true) {
  64. textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
  65. }
  66. }
  67. span[a] = document.createElement("span");
  68. span[a].className = "select";
  69. span[a].id = "select-" + inputs[a].name + a;
  70. span[a].appendChild(textnode);
  71. inputs[a].parentNode.insertBefore(span[a], inputs[a]);
  72. inputs[a].id = inputs[a].name + a;
  73. if(!inputs[a].getAttribute("disabled")) {
  74. inputs[a].onchange = Custom.choose;
  75. } else {
  76. inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
  77. }
  78. }
  79. }
  80. document.onmouseup = Custom.clear;
  81. }
  82. catch(e){ /* */ }
  83. },
  84. pushed: function() {
  85. element = this.nextSibling;
  86. if(element.checked == true && element.type == "checkbox") {
  87. this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
  88. } else if(element.checked == true && element.type == "radio") {
  89. this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
  90. } else if(element.checked != true && element.type == "checkbox") {
  91. this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
  92. } else {
  93. this.style.backgroundPosition = "0 -" + radioHeight + "px";
  94. }
  95. },
  96. check: function() {
  97. element = this.nextSibling;
  98. if(element.checked == true && element.type == "checkbox") {
  99. this.style.backgroundPosition = "0 0";
  100. element.checked = false;
  101. } else {
  102. if(element.type == "checkbox") {
  103. this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
  104. } else {
  105. this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
  106. group = this.nextSibling.name;
  107. inputs = document.getElementsByTagName("input");
  108. for(a = 0; a < inputs.length; a++) {
  109. if(inputs[a].name == group && inputs[a] != this.nextSibling) {
  110. inputs[a].previousSibling.style.backgroundPosition = "0 0";
  111. }
  112. }
  113. }
  114. element.checked = true;
  115. }
  116. },
  117. clear: function() {
  118. inputs = document.getElementsByTagName("input");
  119. for(var b = 0; b < inputs.length; b++) {
  120. if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
  121. inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
  122. } else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
  123. inputs[b].previousSibling.style.backgroundPosition = "0 0";
  124. } else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
  125. inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
  126. } else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
  127. inputs[b].previousSibling.style.backgroundPosition = "0 0";
  128. }
  129. }
  130. },
  131. choose: function() {
  132. option = this.getElementsByTagName("option");
  133. for(d = 0; d < option.length; d++) {
  134. if(option[d].selected == true) {
  135. //
  136. var expr = '#select-' + this.id;
  137. fb.log(expr);
  138. $(expr).text(option[d].childNodes[0].nodeValue);
  139. // bad!
  140. //document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
  141. }
  142. }
  143. }
  144. }
  145. window.onload = Custom.init;