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

251
Visualizações
When a list is selected, sub-list does not refresh in HTML/JavaScript web page

I am trying to write a bot for a website. I am stuck at selecting sub-lists. I can set a value for a list, but unfortunately other list is not update via the selected list.

For example, I set "A" category to value "B" using document.getElementById('A').value = "B"; but sub-list is not updated with "B" models.

I am using Chrome Console to check my coding validations.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This is pretty trivial to do, it really depends where you are loading the derived options from and how (fetch from server, included in DOM etc..). Normally the list is dynamically loaded as JSON and you will parse it and populate the select box with the corresponding values.

Here is a small example of one of many ways to do that - I assume you understand that values is all the required values you are using and loading from your server:

//Load the required values from server or add them to the page statically:
//You can also fetch them one by one... you get the point.
var values = { 
  fruits : [
    { value : "apple", text : "Apple"},
    { value : "orange", text : "Orange"}
  ],
  drinks : [
    { value : "lemonade", text : "Lemonade"},
    { value : "water", text : "Water"},
    { value : "coffee", text : "Coffee"},
    { value : "tea", text : "Tea"}
  ]
};

//The dropdown:
let dropdown = document.getElementById('main-list');

//Attach a `change` event handler:
dropdown.addEventListener(
   'change',
   function() { 
   
     //Clear the populate list first:
     const populate = document.getElementById('populate');
     populate.innerText = null;
     
     //The selected value - which defines what to load:
     const load = this.value;
     
     //Add the options to the populate select element:
     for (const opt of (values[load] || [])) {
        let option = document.createElement('option');
        option.text = opt.text;
        option.value = opt.value;
        populate.add(option);
     }
   },
   false
);
<select id="main-list">
    <option value="none">Please select</option>
    <option value="fruits">Fruits</option>
    <option value="drinks">Drinks</option>
</select>
<select id="populate">
</select>

about 4 years ago · Juan Pablo Isaza Relatório

0

Found the answer:

document.querySelector('#id').dispatchEvent(new Event('change', { 'bubbles': true }))
about 4 years ago · Juan Pablo Isaza Relatório
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