- Tue Dec 29, 2020 3:17 pm
#828792
Is it possible to sort a dropdown list in Javascript? The code below is from the wiki link posted below. I am reading in a json list that is not sorted correctly and either during or after I use code similar to the example below I want to sort it by the text - the "opt.text = aProducts[1];" value. Is this possible in javascript right after or while I load the drop down list?
https://wiki.processmaker.com/index.php/2.0/Dropdown_Boxes_and_Listboxes#Accessing_Dropdowns_with_JavaScript
var aProducts = eval(getValueById("sProducts"));
if (typeof(aProducts) == 'object' && aProducts.length) { //if an array
for (var i=0; i < aProducts.length; i++) {
var opt = document.createElement("OPTION");
opt.value = aProducts[0];
opt.text = aProducts[1];
getField("selectProducts").options.add(opt);
}
}
https://wiki.processmaker.com/index.php/2.0/Dropdown_Boxes_and_Listboxes#Accessing_Dropdowns_with_JavaScript
var aProducts = eval(getValueById("sProducts"));
if (typeof(aProducts) == 'object' && aProducts.length) { //if an array
for (var i=0; i < aProducts.length; i++) {
var opt = document.createElement("OPTION");
opt.value = aProducts[0];
opt.text = aProducts[1];
getField("selectProducts").options.add(opt);
}
}