Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Dynamic Select Options from an Array based on selectedIndex of another drop down using JavaScript

I'm attempting to create a dynamic drop down box based on the selectedIndex of another drop down box. I need help writing a function to achieve this and was hoping someone could help me finish this function. I also need the value and text of the options to be what's in the array. Here's what I have so far:

<form id="myForm">
  <select id="typeSelect">
    <option value="" disabled>--- Select Type ---</option>
    <option value="Type 1">Type 1</option>
    <option value="Type 2">Type 2</option>
    <option value="Type 3">Type 3</option>
  </select>
  <select id="itemSelect"></select>
</form>

< script type = "text/javascript" >
    var typeSelect = document.querySelector('#typeSelect');
    typeSelect.addEventListener('change', onChange);

    function onChange() {
        let itemSelect = document.querySelector('#itemSelect');
        let typeIndex = typeSelect.selectedIndex;
        let itemOptions = {
            0: ['--- Select Type ---'],
            1: ['--- Select Item ---',
                'Item 1',
                'Item 2'
            ],
            2: ['--- Select Item ---',
                'Item 1',
                'Item 2'
            ],
            3: ['--- Select Item ---',
                'Item 1',
                'Item 2'
            ]
        };
        for (var i = 0; i < itemSelect.length; i++) {
            itemSelect.length = 0;
            
        }
    } 
</script>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!