1

I want to change the options of select2 based on the choice in select1. Therefore I made a associative array like this:

var rubrieken = {'1': '<option value="10">Rubriek 10</option><option value="20">Rubriek 20</option>','2': '<option value="30">Rubriek 30</option><option value="40">Rubriek 40</option>'};

So if in select1 option 2 is selected, the current values of select2 have to be replaced with the options with key 2. Im made the Jquery below, but how to extract the options from the array an put them in the select?

var rubrieken = {'1': '<option value="10">Rubriek 10</option><option value="20">Rubriek 20</option>','2': '<option value="30">Rubriek 30</option><option value="40">Rubriek 40</option>'};
$( document ).ready(function() {
    //Bij keuze categorie de rubrieken vullen 
    $("#select1").change(function(){
        //maak de huidige select leeg
        $('#select2').empty();
        //selectie uit array van juiste waarden
        $newoptions = "<option>NewO1</option><option>NewO1</option>";
        //vul de select rubriek op basis van categorie keuze    
        $('select#select2').html($newoptions);
    }); 
});
2
  • and what are values in select1 options . is it 1 and 2 ? Commented Oct 13, 2017 at 9:13
  • 1
    Would comment your html also Commented Oct 13, 2017 at 9:14

2 Answers 2

2

Please try this!

var rubrieken = {
    '1': '<option value="10">Rubriek 10</option><option value="20">Rubriek 20</option>',
    '2': '<option value="30">Rubriek 30</option><option value="40">Rubriek 40</option>'
};

$(function() {
    $("#select1").change(function() {
        var key = $(this).val();
        var newoptions = rubrieken[key];
        $('#select2').html(newoptions);
    }); 
});

Note:

You won't need $('#select2').empty(); because .html() will replace the content.

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

Why not use numbers in the array?
javascript object type have slightly different purpose with normal array, object's key usually to be a string/name. but if you want just normal array, you can define as simple: var rubrieken = ['<option value="10">Rubriek 10</option><option value="20">Rubriek 20</option>','<option value="30">Rubriek 30</option><option value="40">Rubriek 40</option>']; but it'll index from 0.
@JilcoTigchelaar but don't worry, even if you define rubrieken as object like your code, you can read it with normal array reading rubrieken[2]; but not rubrieken.2; (as usually object read is using dot e.g. rubrieken.opt2;)
1

You can access your corresponding option as key-value pair. like if option 1 is selected then your access it like rubrieken["1"]

Way to access object property.

var rubrieken = {'1': '<option value="10">Rubriek 10</option><option value="20">Rubriek 20</option>','2': '<option value="30">Rubriek 30</option><option value="40">Rubriek 40</option>'};
    $( document ).ready(function() {
        //Bij keuze categorie de rubrieken vullen 
        $("#select1").change(function(){
            //maak de huidige select leeg
            $('#select2').empty();
            var selectedValue = $(this).val();
            if(selectedValue){
                var getOptions = rubrieken[selectedValue];
                $('select#select2').html(getOptions);
            }
        }); 
    });

1 Comment

@JilcoTigchelaar : Glad it help you. Accept it as answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.