javascriptjquerydrop-down-menuselectortaffydb

Use dropdown list selection to populate second dropdown list contents


The code below populates the first dropdown with distinct pants brands:

$.each(pantsBrands, function(i){
    var li = $('<li>')
        .appendTo(pantsList);
    var aaa = $('<a>')
        .text(pantsBrands[i])
        .attr('onclick','askPantStyle()')
        .appendTo(li);
});

In this next bit of code, I intend to somehow use the selection above to populate the contents in a second dropdown box composed of the styles relevant to the brand chosen in the first.

var askPantStyle = function(){
    $('#pantStyleDiv').slideDown();
    alert($(this).text());
}

I used the alert to check to see what (this) would return. I had hoped it would be the text content of the first selection, but when the alert box appears, the content is empty.

relevant html below:

<div id='pantsLanding'>
            <p class="lead">Which brand of pants fits you best?</p>
            <p class="lead">
                <!-- Split button -->
                <div class="btn-group">
                    <button type="button" class="btn btn-default">Brand</button>
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <ul id="dropPants" class="dropdown-menu scrollable-menu" role="menu">
                    </ul>
                </div>
            </p>  
        </div>
        <div id='pantStyleDiv'>
            <p class="lead">What style do you prefer?</p>
            <p class="lead">
                <!-- Split button -->
                <div class="btn-group">
                    <button type="button" class="btn btn-default">Style</button>
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <ul id="dropPantStyle" class="dropdown-menu scrollable-menu" role="menu">
                    </ul>
                </div>
            </p>  
        </div>

Solution

  • Added a quick example below of how you could solve this. Bootstrap dropdowns are quite specific in their usage, so I'm using a click event rather than change that is the normal event for selects.

    $('#dropPants').on('click', 'li a', function() { ... });
    

    Also I did not add any data-attributes, as I do not have the full picture of your solution. But I hope you can use this to make things work like you want them to.

    You did not provide any data, so I created an array for the brands, and an object for the styles. Each brand has a property in the object containing an array of styles.

    var pantsBrands = ['levis', 'lee', 'lindeberg'];
    var pantsStyles = { 
            'levis': ['501','502'],
            'lee': ['bruce','kim'],
            'lindeberg': ['lindeberg1','lindeberg2']
    };
    

    Anyway, below is a working example of a Bootstrap dropdown populating another dropdown:

    var pantsBrands = ['levis','lee','lindeberg'];
    var pantsStyles = { 'levis': ['501','502'],
                        'lee': ['bruce','kim'],
                        'lindeberg': ['lindeberg1','lindeberg2']
                      };
    
    $.each(pantsBrands, function(i) {
      $('#dropPants').append('<li><a href="#">'+pantsBrands[i]+'</a></li>');
    });
    
    $('#dropPants').on('click', 'li a', function() {
      $('#dropPantStyle').html('');
      var showStyles = pantsStyles[$(this).text()];
      $.each(showStyles, function(i) {
        $('#dropPantStyle').append('<li><a href="#">'+showStyles[i]+'</a></li>');
      });   
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
    <div id='pantsLanding'>
      <p class="lead">Which brand of pants fits you best?</p>
      <p class="lead">
        <!-- Split button -->
        <div class="btn-group">
          <button type="button" class="btn btn-default">Brand</button>
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
          </button>
          <ul id="dropPants" class="dropdown-menu scrollable-menu" role="menu">
          </ul>
        </div>
      </p>
    </div>
    <div id='pantStyleDiv'>
      <p class="lead">What style do you prefer?</p>
      <p class="lead">
        <!-- Split button -->
        <div class="btn-group">
          <button type="button" class="btn btn-default">Style</button>
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
          </button>
          <ul id="dropPantStyle" class="dropdown-menu scrollable-menu" role="menu">
          </ul>
        </div>
      </p>
    </div>