Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic dropdown options with JQuery

I would like to dynamically change the options of a HTML dropdown menu based on which company a user selects - for example, I have this JSON data:

series: [
{name: 'Company A', product: 'A1'},
{name: 'Company A', product: 'A2'},
{name: 'Company A', product: 'A3',},
{name: 'Company B', product: 'B1'},
{name: 'Company B', product: 'B2'}
]

And I have two dropdown menus:

<div class="col-md-4" >
    <select class="company">
          <option value=''><strong>Name</strong></option>
          <option value="Company A">Company A</option>
          <option value="Company B">Company B</option>
    </select>
</div>

If the user selects Company A, then my 2nd dropdown will show products for that company:

<div class="col-md-4" >
    <select class="product">
          <option value=''><strong>Products</strong></option>
          <option value="A1">A1</option>
          <option value="A2">A2</option>
          <option value="A3">A3</option>
    </select>
</div>

Likewise, if the user selects Company B, the options will be B1, B2.

I am stuck with a function similar to this one (I am new to web dev and an largely unfamiliar with javascript and its syntax):

   $('.company').change(function() {
  company = this.value;
  if (company) {
    Highcharts.each(chart.series, function(ob, j) {
      if (...................) {
        ob.setVisible(true, true)
      } else {
        ob.setVisible(false, false)
      }
    });

  }
})

This function relates to the Highcharts API, but I don't think that that is relevant to the question; I am looking for a more general JQuery/JavaScript answer, it doesn't have to entail Highcharts...

like image 399
mk8efz Avatar asked Jan 17 '26 23:01

mk8efz


2 Answers

As in your try, you should use the .change() jQuery method on the first select element. Then just loop through your object and create the new options.

HTML:

<div class="col-md-4" >
    <select class="company">
          <option value=''><strong>Name</strong></option>
          <option value="Company A">Company A</option>
          <option value="Company B">Company B</option>
    </select>
</div>
<div class="col-md-4" >
    <select class="product">
          <option value=''><strong>Products</strong></option>
    </select>
</div>

JavaScript

var series = [
{name: 'Company A', product: 'A1'},
{name: 'Company A', product: 'A2'},
{name: 'Company A', product: 'A3'},
{name: 'Company B', product: 'B1'},
{name: 'Company B', product: 'B2'}
]

$(".company").change(function(){
    var company = $(this).val();
    var options =  '<option value=""><strong>Products</strong></option>'; //create your "title" option
    $(series).each(function(index, value){ //loop through your elements
        if(value.name == company){ //check the company
            options += '<option value="'+value.product+'">'+value.product+'</option>'; //add the option element as a string
        }
    });

    $('.product').html(options); //replace the selection's html with the new options
});

Here is a working jsFiddle https://jsfiddle.net/6xtzt8k4/

like image 88
Stavros Avatar answered Jan 19 '26 11:01

Stavros


So if i am not mistaken you are looking for something like this.

HTML :

<select id="company"></select>
<select id="product"></select>

jQuery :

var series = [
{name: 'Company A', product: 'A1'},
{name: 'Company A', product: 'A2'},
{name: 'Company A', product: 'A3'},
{name: 'Company B', product: 'B1'},
{name: 'Company B', product: 'B2'}
]

$(function(){
    $("#product").html("<option id=''>Select</option>")
    $("#company").html('<option id="">Select</option>')
    $(series).each(function(index,value){
      if($("#company").find('option[id="'+ value.name +'"]').length == 0){
            $("#company").append('<option id="'+ value.name +'">'+value.name+'</option>')
      } 
  }) 
})

$("#company").change(function(){
    var companyId = $(this).val()
    $("#product").html("<option id=''>Select</option>")
        if(companyId != ''){
            $(series).each(function(index,value){
              if(value.name == companyId){
                $("#product").append('<option id="'+value.product+'">'+ value.product+'</option>')
               }
             })
         }
    })

Where you have in document ready filling companies select , only validating if already exists inside select since there is repeating of companies.

On change , we are clearing product select , and filling with producs , where companyName == first select value.

Here is jsFiddle https://jsfiddle.net/noitse/coz3q274/1/

like image 42
noitse Avatar answered Jan 19 '26 13:01

noitse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!