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

145
Visualizações
Alternative to onclick for select tag

I have a Javascript function that changes a dropdown menu's options based on some radio buttons that have been selected. I originally had the radio buttons set to onclick to run the function but as it is based on 2 different sets of radio buttons this does not work. I changed the onclick to happen when the user selects the drop down however now you cannot select an actual option as it just resets the dropdown when a selection is made. Is there and alternative I can use?

Genotype <select name="dropdown" id="dropdown" onclick = "DROPDOWN(father);"></select>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of onclick you will need to track onchange, so even if there are some label elements around them, changing them, you will have your code working. As about the solution, you will need to implement a logic that determines the options to be generated upon any possible combination of your radio buttons. The code below is a proof-of-concept.

function refreshOptions() {
    let innerHTML = "<option>Please select</option>";
    for (let item of document.querySelectorAll("input[type=radio]")) {
        if (item.checked) innerHTML += `<option value="${item.value}">${item.value}</option>`
    }
    document.getElementById("myselect").innerHTML = innerHTML;
}
<select id="myselect">
    <option>Please select</option>
    <option value="foo1">foo1</option>
    <option value="bar1">bar1</option>
</select>
<label>foo1 <input onchange="refreshOptions()" type="radio" name="foo" value="foo1" checked></label>
<label>foo2 <input onchange="refreshOptions()" type="radio" name="foo" value="foo2"></label>
<label>bar1 <input onchange="refreshOptions()" type="radio" name="bar" value="bar1" checked></label>
<label>bar2 <input onchange="refreshOptions()" type="radio" name="bar" value="bar2"></label>

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