Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda