I am currently working with dynamic dropdowns that are populated based on values fetched from an api. I am using this jquery cascading dropdown library which works well out of the box. I am trying to figure the right way to select values of the dropdown through a button trigger event. I placed two buttons that when clicked it should select a value from the dropdown. This works well on the first click but on subsequent clicks nothing happens after. What would be the best way to achieve this?
Here is running example: https://jsfiddle.net/3jxvzrmw/2/
const editBtn = document.querySelectorAll('.editButton');
for (const button of editBtn) {
button.addEventListener('click', (e) => {
let make = e.target.dataset.make
$("#dropdowns").cascadingDropdown({
selectBoxes: [{
selector: '.step1',
source: function (request, response) {
$.getJSON("https://private-anon-f7832ff619-carsapi1.apiary-mock.com/cars", request, function (data) {
response($.map(data, function (item, index) {
return {
label: item.make,
value: item.make,
};
}));
}).done(function () {
$("#make").val(make).change();
}).fail(function () {
alert("unable to load data for dropdown make");
});
},
}]
});
});
}