-1

JS newbie here, sorry for dumb question in advance.

I am trying to gather all options from a select element and base the available options in another select element off whatever was selected in the first select element. I get all the options of a select element after waiting 1 second because those options are derived dynamically and aren't there to query immediately. The problem is that I don't know how to return allNameOptions so that I can use it in the following change function. I have tried a return statement but for some reason this returns the value 3. No idea where that's coming from.

Current code:

setTimeout(function () {
    var allNameOptions = $('#cipAreaNames option');
}, 1000);

$('#printStateAbbrev').change(function () {
    $('#cipAreaNames option').remove()
    var selState = $('#printStateAbbrev option:selected').prop('value');
    var availOptions = allNameOptions.filter('.' + selState);
    $.each(availOptions, function (i, j) {
        $(j).appendTo('#cipAreaNames');
    });
});

What I tried (and edited allNameOptions to names in the change function):

var names = setTimeout(function () {
    var allNameOptions = $('#cipAreaNames option');
    return allNameOptions;
}, 1000);
MKF
  • 426
  • 7
  • 24

1 Answers1

0

You need to declare allNameOptions variable outside your function, in global space, without value. Like just var allNameOptions;. Next you can give it a value in one function and use it in another.