javascript select opt group from son sample

As shown in http://jsfiddle.net/DVKMK/3/ and http://stackoverflow.com/questions/13956667/create-html-select-with-optgroup-from-json

<div id="somewhere"></div>

var json = [{"Foo":[{"id":1,"name":"aaa"},{"id":2,"name":"bbb"}]},{"Bar":[{"id":3,"name":"ccc"},{"id":4,"name":"ddd"}]}];

var $select = $("<select>");
$.each(json, function(i, optgroups) {

    $select.appendTo("#somewhere");

    $.each(optgroups, function(groupName, options) {
        var $optgroup = $("<optgroup>", {label: groupName});
        $optgroup.appendTo($select);

        $.each(options, function(j, option) {
            var $option = $("<option>", {text: option.name, value: option.id});
            $option.appendTo($optgroup);
        });
    });
});


Popular posts from this blog

npm refusing to install as a dependency of itself

How to call javascript functions from VB.NET and using WebBrowser Control