Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. var x, i, j, selElmnt, a, b, c;
  2. /*look for any elements with the class "custom-select":*/
  3. x = document.getElementsByClassName("custom-select");
  4. for (i = 0; i < x.length; i++) {
  5. selElmnt = x[i].getElementsByTagName("select")[0];
  6. /*for each element, create a new DIV that will act as the selected item:*/
  7. a = document.createElement("DIV");
  8. a.setAttribute("class", "select-selected");
  9. a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  10. x[i].appendChild(a);
  11. /*for each element, create a new DIV that will contain the option list:*/
  12. b = document.createElement("DIV");
  13. b.setAttribute("class", "select-items select-hide");
  14. for (j = 1; j < selElmnt.length; j++) {
  15. /*for each option in the original select element,
  16. create a new DIV that will act as an option item:*/
  17. c = document.createElement("DIV");
  18. c.innerHTML = selElmnt.options[j].innerHTML;
  19. c.addEventListener("click", function(e) {
  20. /*when an item is clicked, update the original select box,
  21. and the selected item:*/
  22. var y, i, k, s, h;
  23. s = this.parentNode.parentNode.getElementsByTagName("select")[0];
  24. h = this.parentNode.previousSibling;
  25. for (i = 0; i < s.length; i++) {
  26. if (s.options[i].innerHTML == this.innerHTML) {
  27. s.selectedIndex = i;
  28. h.innerHTML = this.innerHTML;
  29. y = this.parentNode.getElementsByClassName("same-as-selected");
  30. for (k = 0; k < y.length; k++) {
  31. y[k].removeAttribute("class");
  32. }
  33. this.setAttribute("class", "same-as-selected");
  34. break;
  35. }
  36. }
  37. h.click();
  38. });
  39. b.appendChild(c);
  40. }
  41. x[i].appendChild(b);
  42. a.addEventListener("click", function(e) {
  43. /*when the select box is clicked, close any other select boxes,
  44. and open/close the current select box:*/
  45. e.stopPropagation();
  46. closeAllSelect(this);
  47. this.nextSibling.classList.toggle("select-hide");
  48. this.classList.toggle("select-arrow-active");
  49. });
  50. }
  51. function closeAllSelect(elmnt) {
  52. /*a function that will close all select boxes in the document,
  53. except the current select box:*/
  54. var x, y, i, arrNo = [];
  55. x = document.getElementsByClassName("select-items");
  56. y = document.getElementsByClassName("select-selected");
  57. for (i = 0; i < y.length; i++) {
  58. if (elmnt == y[i]) {
  59. arrNo.push(i)
  60. } else {
  61. y[i].classList.remove("select-arrow-active");
  62. }
  63. }
  64. for (i = 0; i < x.length; i++) {
  65. if (arrNo.indexOf(i)) {
  66. x[i].classList.add("select-hide");
  67. }
  68. }
  69. }
  70. /*if the user clicks anywhere outside the select box,
  71. then close all select boxes:*/
  72. document.addEventListener("click", closeAllSelect);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement