samwise
samwise

Reputation: 461

YUI 2 Populate Select with JSON

Really simple question but I can't find an answer. I know how to do this in regular javascript and I know how to do this in jquery. Right now if I have this:

var states = [{"name":"alaska","id":1},{"name":"alabama","id":2];

Is there an YUI based method to push that into a select box, to set the selected item, to get the selected item back? If so, where's the online example? If not I'll just use javascript.

Upvotes: 1

Views: 538

Answers (1)

Zlatko
Zlatko

Reputation: 19578

Yes, there is. You can do something like this:

var buttonMenu = [

        { text: "Alaska", value: 1, onclick: { fn: onStateClick } },
        { text: "Alabama", value: 2, onclick: { fn: onStateClick } },
        ...];

.. and then use this in a YUI menu button configuration:

var statesButton = new YAHOO.widget.Button({ type: "menu", label: "Alabama", name: "mymenubutton", menu: buttonMenu, container: containerElement });

More details on the YUI button example page. Hope that helps.

Upvotes: 2

Related Questions