Jump to content


Get value of custom select menu

select menu html css

  • Please log in to reply
4 replies to this topic

#1 georgene


    Rank I Member

  • Members
  • Pip
  • 28 posts

Posted 21 December 2020 - 03:33 PM

W3shools made a custom select menu. However, they don't mention how to get the value of the select menu as a variable in javascipt. Normally, I would use var choice = document.getElementsByClassName("custom-select").value   but it doesn't work with their custom version.  

The url of the source is https://www.w3school...stom_select.asp and the source code is :

Thank you.

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
/*the container must be positioned relative:*/
.custom-select {
  position: relative;
  font-family: Arial;

.custom-select select {
  display: none; /*hide original SELECT element:*/

.select-selected {
  background-color: DodgerBlue;

/*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;

/*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;

/*style the items (options), including the selected item:*/
.select-items div,.select-selected {
  color: #ffffff;
  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: DodgerBlue;
  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);


<h2>Custom Select</h2>

<!--surround the select box with a "custom-select" DIV element. Remember to set the width:-->
<div class="custom-select" style="width:200px;">
    <option value="0">Select car:</option>
    <option value="1">Audi</option>
    <option value="2">BMW</option>
    <option value="3">Citroen</option>
    <option value="4">Ford</option>
    <option value="5">Honda</option>
    <option value="6">Jaguar</option>
    <option value="7">Land Rover</option>
    <option value="8">Mercedes</option>
    <option value="9">Mini</option>
    <option value="10">Nissan</option>
    <option value="11">Toyota</option>
    <option value="12">Volvo</option>

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;
  /*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++) {
            this.setAttribute("class", "same-as-selected");
  a.addEventListener("click", function(e) {
      /*when the select box is clicked, close any other select boxes,
      and open/close the current select box:*/
function closeAllSelect(elmnt) {
  /*a function that will close all select boxes in the document,
  except the current select box:*/
  var x, y, i, xl, yl, arrNo = [];
  x = document.getElementsByClassName("select-items");
  y = document.getElementsByClassName("select-selected");
  xl = x.length;
  yl = y.length;
  for (i = 0; i < yl; i++) {
    if (elmnt == y[i]) {
    } else {
  for (i = 0; i < xl; i++) {
    if (arrNo.indexOf(i)) {
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);


#2 Krydos


    Rank X Member

  • Root Admin
  • 21,950 posts
  • Gender:Male

Posted 21 December 2020 - 09:57 PM

What is the URL where you have this hosted on our servers?

#3 georgene


    Rank I Member

  • Members
  • Pip
  • 28 posts

Posted 22 December 2020 - 12:19 AM


#4 wolstech


    Rank X Member

  • Root Admin
  • 13,561 posts
  • Gender:Male
  • Location:Pennsylvania

Posted 22 December 2020 - 12:58 AM

Assign an ID to the <select> element (e.g. <select id="carDD"> ) and reference it as:


Please do not PM me for support. Posting on our forums lets all of us learn.
Do you play Minecraft? | Server Monitors | And the Dumbass of the Decade award goes to...

#5 georgene


    Rank I Member

  • Members
  • Pip
  • 28 posts

Posted 22 December 2020 - 02:15 AM

Oh! It all makes sense now! The custom select id wasn't on the select tag it was on the div.

Thank you.

Also tagged with one or more of these keywords: select, menu, html, css

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users