June 26, 201115 yr Hi everybody. I'm being forced to work with javascript at the moment which is not by any stretch of the imagination my main strength. Currently I have a function that is pulling up accounts (pages, apps etc) from facebook profiles using the graph api. My question is a basic syntax related one which I am sure is simple enough but for some reason I can't get my head around it. Here is my code right now: function pages() { FB.api('/me/accounts', 'get', {}, function(response) { if(response.data) { for(var i=0,page; page=response.data[i]; i++) { if(page.name) { $('.fb_pages').wrapInner(function() { return '<option value="' + page.id + ' - ' + page.access_token + '">' + page.name + '"></option>'; }); } } } }); }; What the above does is output something like this: <select class="fb_pages"> <option value="foo">Foo<option value="bar">Bar<option value="argh!">ARGH!</option></option></option> </select> What I would like is the following: <select class="fb_pages"> <option value="foo">Foo</option> <option value="bar">Bar</option> <option value="wahey">Wahey!</option> </select> Thanks in advance for any help! Also please excuse the messy inclusion of jquery in the middle... it seemed to be the best way to target a class as I already had jquery loaded.
June 27, 201115 yr .wrapInner() is the wrong function to use. It's meant for putting tags around an element, not adding content. Change this line: $('.fb_pages').wrapInner(function() { To: $('.fb_pages').append(function() {
June 27, 201115 yr Author .wrapInner() is the wrong function to use. It's meant for putting tags around an element, not adding content. Change this line: $('.fb_pages').wrapInner(function() { To: $('.fb_pages').append(function() { Thanks so much Spitfire! That seems embarrassingly obvious in retrospect! Here is the complete working function: function pages() { FB.api('/me/accounts', 'get', {}, function(response) { if(response.data) { for(var i=0,page; page=response.data[i]; i++) { if(page.name) { $('.fb_pages').append(function() { return '<option value="' + page.id + ' - ' + page.access_token + '">' + page.name + '</option>'; }); } } } }); };
Create an account or sign in to comment