Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript for loop

Featured Replies

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.

.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() {

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.