skip to Main Content

Is there a way to use Javascript to extract a list of all the items in a dropdown list please?

So, for example, for this dropdown, I would want to generate something like this:

  • 4-6 years
  • 8-10 years
  • 3-4 years
  • 10-12 years
  • 6-8 years

(or it can all be on one line, it doesn’t have to be a bulleted list.)

2

Answers


  1. Chosen as BEST ANSWER

    I did attempt it myself.

                var a, i, options;
                a = document.getElementById("size-select");
                options = "";
                for (i = 0; i < a.length; i++) {
                   options = options + "<br> " + a.options[i].text;
                }
                document.write("DropDown Options: "+options);
             }
    

    am I along the right lines?


  2. Just get the select element and then you have access to it’s options:

    const select = document.getElementById("size-select");
    const options = [...select.options].map(i => i.value);
    
    // if you want to write them to the document:
    document.write(options.join('<br>'));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search