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

151
Views
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 answers
Answer question

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 Report
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!