select custom
by Imri Paloja
HTML
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"
/>
<div class="container">
<!-- Wrap the select box in a 'custom-select' DIV element. Remember to set the width:-->
<div class="custom-select">
<select name="domain" id="domain" form="access_form">
<option value="43">789.eurocms.eu</option>
<option value="17">acc.eurocms.eu</option>
<option value="36">apple.eurocms.eu</option>
<option value="34">banana.eurocms.eu</option>
<option value="4">dev.eurocms.eu</option>
<option value="2">module.eurocms.eu</option>
<option value="25">plugins.eurocms.eu</option>
<option value="13">prod.eurocms.eu</option>
<option value="22">repo.eurocms.eu</option>
<option value="1" selected="">test.eurocms.eu</option>
<option value="3">www.eurocms.eu</option>
</select>
</div>
</div>
CSS
html,
body {
color: #454545;
}
.container {
margin-top: 5%;
}
/*the container must be positioned relative:*/
.custom-select {
position: relative;
border: 1px solid #CCCCCC;
color: #454545;
}
.custom-select select {
display: none; /*hide original SELECT element:*/
border: 1px solid #CCCCCC;
color: #454545;
}
.select-selected {
background-color: #f9f9f9;
border: 1px solid #CCCCCC;
color: #454545;
}
/*style the arrow inside the select element:*/
.select-selected:after {
position: absolute;
content: '';
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
color: #454545;
}
/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
border-color: transparent transparent #fff transparent;
top: 7px;
color: #454545;
}
/*style the items (options), including the selected item:*/
.select-items div,
.select-selected {
color: #454545;
padding: 8px 16px;
border: 1px solid transparent;
border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
cursor: pointer;
user-select: none;
}
/*style items (options):*/
.select-items {
position: absolute;
background-color: #f9f9f9;
top: 100%;
left: 0;
right: 0;
z-index: 99;
}
/*hide the items when the select box is closed:*/
.select-hide {
display: none;
}
.select-items div:hover,
.same-as-selected {
background-color: rgba(0, 0, 0, 0.1);
}
JavaScript
var x, i, j, l, ll, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName('custom-select');
l = x.length;
for (i = 0; i < l; i++) {
selElmnt = x[i].getElementsByTagName('select')[0];
ll = selElmnt.length;
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement('DIV');
a.setAttribute('class', 'select-selected');
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/*for each element, create a new DIV that will contain the option list:*/
b = document.createElement('DIV');
b.setAttribute('class', 'select-items select-hide');
for (j = 1; j < ll; j++) {
/*for each option in the original select element,
create a new DIV that will act as an option item:*/
c = document.createElement('DIV');
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener('click', function (e) {
/*when an item is clicked, update the original select box,
and the selected item:*/
var y, i, k, s, h, sl, yl;
s = this.parentNode.parentNode.getElementsByTagName('select')[0];
sl = s.length;
h = this.parentNode.previousSibling;
for (i = 0; i < sl; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName('same-as-selected');
yl = y.length;
for (k = 0; k < yl; k++) {
y[k].removeAttribute('class');
}
this.setAttribute('class', 'same-as-selected');
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener('click', function (e) {
/*when the select box is clicked, close any other select boxes,
and open/close the current select box:*/
e.stopPropagation();
...