javascriptiphonedashcode

How to set popup menu values programmatically in Dashcode


I am trying to set the values for a popup menu in Dashcode programmatically. I can change the text/value of some statically defined default ones (from the inspector), but not add or remove them. When the view is initialised it must take a variable number of options.

    var popup = document.getElementById('popup');
    //popup.options = []; /* Doesn't clear the list */
    //popup.options.length = 0; /* Doesn't clear the list */
    popup.options[0].text = "Option 1";
    popup.options[0].value = "123";

How can I modify the list? (LMGTFY answers not required :)


Solution

  • I solved it like this in the end:

    //remove all
    if (popup.hasChildNodes()) {
        while (popup.childNodes.length >= 1) {
            popup.removeChild(popup.firstChild);       
        }
    }
    
    //add new
    $.each(items, function(i, item) {
        var option = document.createElement("option");
        option.text = item.name;
        option.value = item.id;
        popup.appendChild(option);      
    });