Build a dynamic select object using JSON on Facebook

Maybe if you are building a Facebook Aplicattion and you need to get the options from a JSON Response to create a select object, that post if for you.

The json response that we hace from our server server side is the next:

{"1":"MacOs","2":"Linux ","3","Windows"}

An that is the HTML and FBJS code (yeah, another way to hack Javascript) to build our object:

<input type="button" onclick="buildObject()" value="Build">
<div id="selectZone"></div>

<script type="text/javascript">
function buildObject(){
    var ajax = new Ajax();
    ajax.responseType = Ajax.JSON;
    ajax.ondone = function(data){
        select = document.createElement("select");
        for(var i in data){
            o = document.createElement("option");
            o.setValue(i);
            o.setTextValue(data[i]);
            select.appendChild(o);
        }
        document.getElementById('selectZone').setTextValue('');
        document.getElementById('selectZone').appendChild(select);
    }
    ajax.post('http://YOUR_URL/');
}
</script>

And that's all :).

About the author

josue

Josue is a Web developer working on Astrata with good friends, he love the music and the science, learn always is funny.

Comments