Editing dropdown values without having to create a new dropdown every time

93 views Asked by At

Within the code.org javascript editor for the applab, there is the ability to create a dropdown by creating a dropdown by:

dropdown('id', 'option1', 'option2', ...);

I would like to know if there is a way to edit the list, treating it something like an array:

remove('id', 'position');

and:

insert('id', 'position', 'item1', 'item2'...);

any help would be appreciated, as I can't find any way to do this in other places.

1

There are 1 answers

0
Cengiz On

You can convert dropdown to an array using getProperty. Then, edit the array, and convert it back to a dropdown with setProperty. Here's the code;

dropdown("id", "a", "b", "c");
function editDropdown() {
  var myDropdown = getProperty("id", "options");
  //add someting to your dropdown.
  appendItem(myDropdown, "e");
  //remove something from your dropdown.
  removeItem(myDropdown, "a");

  setProperty("id", "options", myDropdown);
  
}
editDropdown();