I have two dropdown menus and I would like to store the values of the user selected value. I am making use of selectize library for the dropdowns. Html:
<div style="max-width: 200px">
<select
id="dropdown-1"
multiple="multiple">
<option value="bob">bob</option>
<option value="mary">mary</option>
</select>
</div>
<div style="max-width: 200px">
<select
id="dropdown-2"
multiple="multiple">
<option value="bread">bread</option>
<option value="bread">bread</option>
</select>
</div>
Javascript:
//variables to store the values
let nameValues = "";
let foodValues = "";
//The selectize library
$(document).ready(function ($) {
let $market = $("#dropdown-1").selectize({
sortField: "text",
placeholder: "Choose a name...",
onChange: (value) => {
nameValues += value.join("&");
},
});
let $msm = $("#dropdown-2").selectize({
sortField: "text",
placeholder: "Choose a food value...",
onChange: (value) => {
foodValues += value.join("&");
},
});
console.log(nameValues)
console.log(foodValues)
Putting the console.log(nameValues) and console.log(foodValues) works inside of the onchange function of each. But I want to be able to get the values and use it in another function like this:
const getDropDownValues = (dropval_1, dropval_2)=>{
console.log(dropval_1 + dropval_2)
}
getDropDownValues(nameValues, foodValues).....
when I console.log each variable it shows empty no value is displayed. what am I doing wrong is there a way to better achieve this?
console.log(nameValues)inside theonChangemethodgetDropDownValues(nameValues, foodValues)after theonChangeevent of the dropdowns have been triggered. Eg. inside an event listener or something similar otherwise you will get invalid values.