I am looking to automate the process of selecting random items from a dropdown list. Currently, it is selecting items in order from first to last, but I would like it to select items randomly.
/* function to automatically select items from DropDownList1 */
function selectFromDropdown(selector, text) {
$(selector).find('option').each(function() {
if ($(this).text() == text) {
$(selector).val($(this).val());
return false;
}
})
}
$(document).ready(function() {
let numberOfTimes = 0;
const time = 1000 //3s
let values = [];
$('#DropDownList1').find('option').each(function() {
values.push($(this).text())
});
console.log(values);
const interval = setInterval(function() {
selectFromDropdown('#DropDownList1', values[numberOfTimes])
if (numberOfTimes == values.length - 1) {
clearInterval(interval);
} else {
numberOfTimes = numberOfTimes + 1;
}
},
time);
});
To see the code in action, visit: https://jsfiddle.net/lucasangelo_/17Lgr0kc/6/