API Noob API Noob - 2 months ago 15
jQuery Question

How can I append a value to dynamically generated options with javascript?

I'm creating dynamic options from Facebook array. I want to append the

access_token
to the selected page option value. After looking around, I don't understand how to modify my code to make it work.

Markup

<!-- DISPLAY CONNECT BUTTON -->
<button type="button" id="connect" onclick="login();return false;">

<!-- DISPLAY SELECT OPTION LIST-->
<div id="pageContainer" style="display:none;">
<select id="pages" style="display:none;"></select>
</div>


Javascript

//open login dialog onClick
function login() {
FB.login(function(){
FB.api('/me/accounts/', function(response) {
var pages = document.getElementById('pages');
var pageContainer = document.getElementById('pageContainer');
pages.style.display = 'block';
pageContainer.style.display = 'block';
document.getElementById("connect").style.display="none";
for(var i =0; i < response.data.length; i++)
{
pages[i] = new Option(response.data[i].name);
}
}
);

}, {scope: 'manage_pages'});
}

Answer Source

The constructor of the Option object looks like this:

new Option(text, value, defaultSelected, selected);

If you want to set the value of the new option you create just use the second parameter to set the value:

pages[i] = new Option(response.data[i].name, VALUE);

(I'm not sure where you take the value from, just put it there).