Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependent Dropdown #15

Closed
XzAeRo opened this issue Aug 4, 2016 · 1 comment
Closed

Dependent Dropdown #15

XzAeRo opened this issue Aug 4, 2016 · 1 comment
Labels
enhancement Pull request that improves existing functionality

Comments

@XzAeRo
Copy link

XzAeRo commented Aug 4, 2016

It would be really nice to have a feature to make dependent dropdowns. For example, let's imagine we have 2 dropdowns:

  • Dropdown 1 is independent. Dropdown 2 depends on dropdown 1 value.
  • Dropdown 1 is enabled by default while dropdown 2 is disabled until dropdown 1 has a selected value.
  • An event is fired after a value has been selected in dropdown 1.
  • The default behavior of the selected-value-event should be: if a non-null value has been selected in dropdown 1, then enable dropdown 2.
  • An optional anonymous function should be passed to allow custom validations to enable or reject the enabling of the dropdown 2.

Just an idea, but I would really love a functionality like this since mostly all of the existing solutions require jQuery.

@jshjohnson jshjohnson added the enhancement Pull request that improves existing functionality label Aug 4, 2016
@jshjohnson
Copy link
Collaborator

This is technically already possible. I've made an example below for you:

<label for="cities">States</label>
<select name="cities" id="cities" placeholder="Choose a state">
    <option value="Texas">Texas</option>
    <option value="Chicago">Chicago</option>
    <option value="New York">New York</option>
    <option value="Washington">Washington</option>
    <option value="Michigan">Michigan</option>
</select>

<label for="boroughs">Boroughs</label>
<select name="boroughs" id="boroughs" placeholder="Choose a borough">
    <option value="The Bronx">The Bronx</option>
    <option value="Brooklyn">Brooklyn</option>
    <option value="Manhatten">Manhatten</option>
    <option value="Queens">Queens</option>
    <option value="Staten Island">Staten Island</option>
</select>
document.addEventListener('DOMContentLoaded', function() {
    var cities = new Choices(document.getElementById('cities'), {
        callbackOnChange: function(value) {
            if(value === 'New York') {
                boroughs.enable();
            } else {
                boroughs.disable();
            }
        }
    });

    var boroughs = new Choices(document.getElementById('boroughs')).disable();
});

You could of course do some fancier validation on the value but you get the idea!

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Pull request that improves existing functionality
Projects
None yet
Development

No branches or pull requests

2 participants