I'm trying to create a script that randomly selects an option from a drop-down list.

For that I use a randomly generated number as the index of the set value. The issue is that if it tries to select an item that doesn't exist (like, if the randomly generated number is 6 and the list only has 5 options), the script stops and doesn't run the rest of the code.

So it needs to generate a random integer in the range from 1 to the exact number of options. For now I have hard coded the range in the script, so it works ok in this specific case, but I would like it to work for whatever number of options the drop-down may have, so it continues working if the number of options is changed.

So I would need to get the number of options in the drop-down to set as the maximum value of the range to generate a random number from. But I can't find anywhere a way to get the "size" or "length" of the drop-down options. How do I do that?

1 Answer 1


I ended up making a custom function that gets the number of values in a dropdown effect. I have no idea if this is a good way of doing it but it worked for me! :)

function getDropdownLength(dropdownMenu) {
    var length = 1;
    var originalValue = dropdownMenu.value;

    while (true) {
        try {
            dropdownMenu.setValue(dropdownMenu.value + 1);
        catch (e) {

    return length;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.