I am working on a Google Chrome Extension. So far I have no plans of making it for other browsers (yet) so browser compatibility isn't an issue. I have a lot of CSS styling going on the extension and have chosen to follow W3 Schools splendid guide on custom select boxes - https://www.w3schools.com/howto/howto_custom_select.asp. So far so good! However, this is for a calculator extension, and I really need the users to be able to tab-shift with the keyboard between different input fields.
I am unsure how to get this working?
Is it simply a matter of something I have missed regarding the tabindex, OR do I need to add Javascript code with event-listeners and constants for the keys ?
So far I have setup the design for the element and added "tabindex" to the dropdown. The select box is "selectable" with tab - but you can't select anything with the keyboard. Look here at my codepen: https://codepen.io/lars-ejaas/pen/KKKBmNp
<DIV id=settings>
<H1>Settings</H1>
<p>Decimal separator</p><div class="custom-select" style="width:88px; margin-left: 10px; display: inline-flex">
<select id="seperator" tabindex="-1">
<option value="SELECT">POINT</option>
<option value="COMMA">COMMA</option>
<option value="POINT">POINT</option>
</select>
</div>
<br>Enable mousescroll in input fields <input type="checkbox" id="scroll">
</DIV>
<STYLE>
/* Remove blue ring around focused checkbox */
input:focus {
border-color: #333333 !important;
outline-color: #333333 !important;
}
/* Remove blue ring around focused url's */
a:focus {
outline-color: #333333 !important;
}
.custom-select {
position: relative;
font-family: Oswald;
}
.custom-select select {
display: none; /*hide original SELECT element:*/
}
.select-selected {
background-image: -webkit-repeating-linear-gradient(left, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 6%, hsla(0,0%,100%, 0.1) 7.5%), -webkit-repeating-linear-gradient(left, hsla(0,0%, 0%,0) 0%, hsla(0,0%, 0%,0) 4%, hsla(0,0%, 0%,0.03) 4.5%), -webkit-repeating-linear-gradient(left, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 1.2%, hsla(0,0%,100%,0.15) 2.2%), linear-gradient(180deg, hsl(0,0%,78%) 0%, hsl(0,0%,82%) 78%, hsl(0,0%,78%) 53%, hsl(0,0%,70%)100%);
box-shadow: rgba(38, 38, 38, 0.1) 0px 0px 0px 3px inset, rgba(38, 38, 38, 0.6) 0px -1px 5px 4px inset, rgba(0, 0, 0, 0.25) 0px -1px 0px 7px inset, rgba(255, 255, 255, 0.7) 0px 2px 1px 7px inset;
height: 26px;
width: 46px;
border-radius: 20px;
padding: 4px 19px 4px 19px;
border: 1px;
color: #333;
}
.select-selected:focus {
outline: none;
border-width: 3px;
height: 26px;
border-style: solid;
border-color: rgb(102, 102, 102);
border-image: initial;
padding: 1px 16px;
}
/*style the arrow inside the select element:*/
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 11px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #333 transparent transparent transparent;
}
/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
border-color: transparent transparent #333 transparent;
top: 7px;
right: 11px;
}
/*style the items (options), including the selected item:*/
.select-items div {
color: #333;
padding: 6px 16px;
cursor: pointer;
/*user-select: none;*/
}
/*style items (options):*/
.select-items {
position: absolute;
background-color: white;
box-shadow: rgba(0, 0, 0, 0.8) 2px 2px 8px 0px inset;
top: 100%;
z-index: 99;
border-radius: 20px;
border-style: outset;
border-width: 3px;
outline: none;
}
/*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);
border-radius: 10px;
}
.select-items:focus, .select-items:active {
outline: none;
}
</STYLE>
var x, i, j, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.setAttribute("tabindex", "0");
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 < selElmnt.length; 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;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
for (k = 0; k < y.length; 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();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/*a function that will close all select boxes in the document,
except the current select box:*/
var x, y, i, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
for (i = 0; i < y.length; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < x.length; i++) {
if (arrNo.indexOf(i))
x[i].classList.add("select-hide");
}
}
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);
I need the user to be able to tab to the select box - open the dropdown with the space key, select the desired option with the arrow-keys and select it with "enter" - just as a normal select box. Right now you can only "tab" to the select-box.