Advertisement
afsarwebdev

JS Select Option Background Color Change

Jun 9th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. //HTML
  2. <div class="ic-select">
  3. <select>
  4. <option value="0">Subject</option>
  5. <option value="1">Subject One</option>
  6. <option value="2">Subject Two</option>
  7. <option value="3">Subject Three</option>
  8. <option value="4">Subject Four</option>
  9. </select>
  10. </div>
  11.  
  12. //CSS
  13.  
  14. .ic-select {
  15. position: relative;
  16. height: auto;
  17. padding: 0px;
  18. margin-bottom: 30px;
  19. text-align: left;
  20. }
  21. .ic-select select {
  22. display: none;
  23. /*hide original SELECT element:*/
  24. }
  25. .select-selected {
  26. background-color: #fff;
  27. color: rgba(0, 0, 0, 0.8) !important;
  28. }
  29. /*style the arrow inside the select element:*/
  30. .select-selected:after {
  31. position: absolute;
  32. content: "";
  33. top: 14px;
  34. right: 10px;
  35. width: 0;
  36. height: 0;
  37. border: 6px solid transparent;
  38. }
  39. .select-selected:before {
  40. position: absolute;
  41. content: "";
  42. top: 22px;
  43. right: 10px;
  44. width: 0;
  45. height: 0;
  46. border: 6px solid transparent;
  47. border-color: transparent transparent #29a9a8 transparent;
  48. transform: rotate(180deg);
  49. }
  50. /*point the arrow upwards when the select box is open (active):*/
  51. .select-selected.select-arrow-active:before {
  52. border: none;
  53. }
  54. .select-selected.select-arrow-active:after {
  55. border-color: transparent transparent #29a9a8 transparent;
  56. top: 14px;
  57. }
  58. /*style the items (options), including the selected item:*/
  59. .select-items div,.select-selected {
  60. color: #ffffff;
  61. padding: 8px 16px;
  62. border: 1px solid #29a9a8;
  63. cursor: pointer;
  64. }
  65. /*style items (options):*/
  66. .select-items {
  67. position: absolute;
  68. background-color: #29a9a8;
  69. top: 100%;
  70. left: 0;
  71. right: 0;
  72. z-index: 99;
  73. }
  74. /*hide the items when the select box is closed:*/
  75. .select-hide {
  76. display: none;
  77. }
  78. .select-items div:hover, .same-as-selected {
  79. background-color: rgba(0, 0, 0, 0.1);
  80. }
  81. .ic-contact-form input {
  82. width: 99%;
  83. }
  84.  
  85. //JS (use external js file like "selectoption.js")
  86.  
  87. var x, i, j, selElmnt, a, b, c;
  88. /*look for any elements with the class "ic-select":*/
  89. x = document.getElementsByClassName("ic-select");
  90. for (i = 0; i < x.length; i++) {
  91. selElmnt = x[i].getElementsByTagName("select")[0];
  92. /*for each element, create a new DIV that will act as the selected item:*/
  93. a = document.createElement("DIV");
  94. a.setAttribute("class", "select-selected");
  95. a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  96. x[i].appendChild(a);
  97. /*for each element, create a new DIV that will contain the option list:*/
  98. b = document.createElement("DIV");
  99. b.setAttribute("class", "select-items select-hide");
  100. for (j = 0; j < selElmnt.length; j++) {
  101. /*for each option in the original select element,
  102. create a new DIV that will act as an option item:*/
  103. c = document.createElement("DIV");
  104. c.innerHTML = selElmnt.options[j].innerHTML;
  105. c.addEventListener("click", function(e) {
  106. /*when an item is clicked, update the original select box,
  107. and the selected item:*/
  108. var y, i, k, s, h;
  109. s = this.parentNode.parentNode.getElementsByTagName("select")[0];
  110. h = this.parentNode.previousSibling;
  111. for (i = 0; i < s.length; i++) {
  112. if (s.options[i].innerHTML == this.innerHTML) {
  113. s.selectedIndex = i;
  114. h.innerHTML = this.innerHTML;
  115. y = this.parentNode.getElementsByClassName("same-as-selected");
  116. for (k = 0; k < y.length; k++) {
  117. y[k].removeAttribute("class");
  118. }
  119. this.setAttribute("class", "same-as-selected");
  120. break;
  121. }
  122. }
  123. h.click();
  124. });
  125. b.appendChild(c);
  126. }
  127. x[i].appendChild(b);
  128. a.addEventListener("click", function(e) {
  129. /*when the select box is clicked, close any other select boxes,
  130. and open/close the current select box:*/
  131. e.stopPropagation();
  132. closeAllSelect(this);
  133. this.nextSibling.classList.toggle("select-hide");
  134. this.classList.toggle("select-arrow-active");
  135. });
  136. }
  137. function closeAllSelect(elmnt) {
  138. /*a function that will close all select boxes in the document,
  139. except the current select box:*/
  140. var x, y, i, arrNo = [];
  141. x = document.getElementsByClassName("select-items");
  142. y = document.getElementsByClassName("select-selected");
  143. for (i = 0; i < y.length; i++) {
  144. if (elmnt == y[i]) {
  145. arrNo.push(i)
  146. } else {
  147. y[i].classList.remove("select-arrow-active");
  148. }
  149. }
  150. for (i = 0; i < x.length; i++) {
  151. if (arrNo.indexOf(i)) {
  152. x[i].classList.add("select-hide");
  153. }
  154. }
  155. }
  156. /*if the user clicks anywhere outside the select box,
  157. then close all select boxes:*/
  158. document.addEventListener("click", closeAllSelect);
  159.  
  160. //Max-hieght defiend
  161.  
  162. $(function(){
  163. $(".select-selected").click(function(){
  164. if ($(this).next('.select-items').height() > 200) {
  165. $(this).next('.select-items').css({"maxHeight":"200px", "overflow-y":"scroll"});
  166. }
  167. });
  168. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement