views:

555

answers:

3

I am trying to set the values for a popup menu in Dashcode programatically. 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 :)

James

A: 

Just a stab but couldn't you build the whole popup in JavaScript and set the number of items in the drop down / popup by passing parameters. Then when you want to change any of the items you can call the JavaScript with new parameters?

Or did i misunderstand the question.

PurplePilot
+1  A: 

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);      
});
jgubby
A: 

hi jgubby,

when i try to bind the pop-up menu items dynamically similar as you do, i see the values i assign to pop-up menu items are correct, but non of the items are displayed in the pop-up.

Is there something i'm missing ? Here is my code:

var ddlCurrencyCode = document.getElementById('ddlCurrencyCode'); if (ddlCurrencyCode.hasChildNodes()) { while (ddlCurrencyCode.childNodes.length >= 1) { ddlCurrencyCode.removeChild(ddlCurrencyCode.firstChild);
} }

for (var i = 0; i < result.rows.length; ++i) { var row = result.rows.item(i);
var ddlItem = document.createElement("ddlItem"); ddlItem.text = row['key']; ddlItem.value = row['value']; ddlCurrencyCode.appendChild(ddlItem);
}

oxomoxo