I'd like to create a select
element with a list of a user's Facebook friends (obtained as a JSON object). I hardcode <select id=friends></select>
into my HTML, then use the following Javascript code to parse the JSON and insert each friend as an option
of the select
element:
var msgContainer = document.createDocumentFragment();
for (var i = 0; i < response.data.length; i++) {
msgContainer.appendChild(document.createTextNode('<option value='+response.data[i].id+'>'+response.data[i].name+'</option>'));
}
document.getElementById(friends).appendChild(msgContainer);
This almost works, except that it inserts <
and >
instead of <
and >
. How can I fix it, and is there a more efficient way to insert multiple HTML elements using pure Javascript (not JQuery)?