Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using the facebook Api to get some album names and their cover photos. From this names and photos i am trying to create a jquery Mobile page that presents them in a list.

Some of my javascript code looks like this :

// Additional initialization code such as adding Event Listeners goes here
            FB.api('593959083958735/albums', function(response) {
                if(!response || response.error) {
                    // render error
                    alert("Noo!!");
                } else {
                    // render photos
                    for(i=0; i<response.data.length; i++){
                        albumName[i] = response.data[i].name;
                        albumCover[i] = response.data[i].cover_photo;
                        albumId[i] = response.data[i].id;

                        FB.api( albumCover[i], function(response) {
                            if(!response || response.error) {
                                // render error
                                alert("Noo!!");
                            } else {
                               // render photos
                               document.getElementById('coverPhoto').src = response.picture;
                            }
                        });


                        $("ul").append('<li>'+
                        '<a href="testFile.HTML" data-transition="slidedown">'+
                          '<img src= "nothing.jpg" id = "coverPhoto" />'+
                          '<h2>' + albumName[i] + '</h2>'+
                          '<p> Test Paragraph</p>'+
                        '</a>'+
                         '</li>')
                     .listview('refresh');

                    }
                }
            });

Array AlbumName[] has the names of the albums , and repsonse.picture has the cover picture of every album.

As you can see in then i dynamically create a listView with the names and the pictures i get from the call. However THIS is the result . The names of the albums are all ok , but the photos are messed up. On the "network" tab i see that i get all the cover photos from the albums. But it seems that i overwrite the cover photo only in the first cell of the listView. Why though?

share|improve this question
1  
HTML IDs must be unique. –  Dark Falcon Jun 19 '13 at 15:20

4 Answers 4

up vote 1 down vote accepted

The issue is the way you're rendering your photos. You are selecting element by ID but all the elements have the same ID. Each element should have a unique ID or else your selector won't work.

You should render the photo into your template instead of rendering the template and then replacing the src of the image. Maybe something like this:

// Additional initialization code such as adding Event Listeners goes here

var albums; // a place to get album info outside this function

FB.api('593959083958735/albums', function(response) {
  if(!response || response.error) {
    // render error
    alert("Noo!!");
  } else {
    // render photos
    for(i=0; i<response.data.length; i++){
      albums[albumID] = response.data[i] // for accessing the album info eslewhere

      var albumName = response.data[i].name;
      var albumCover = response.data[i].cover_photo;
      var albumID = response.data[i].id;

      FB.api( albumCover, function(response) {
        if(!response || response.error) {
          // render error
          alert("Noo!!");
        } else {
          // render photos
          $("ul").append('<li>'+
            '<a href="testFile.HTML" data-transition="slidedown">'+
            '<img src="'+response.picture+'" id="coverPhoto-'+albumID+'" />'+
            '<h2>'+albumName+'</h2>'+
            '<p>Test Paragraph</p>'+
            '</a>'+
            '</li>')
          .listview('refresh');
        }
      });
    }
  }
});
share|improve this answer
    
Well if i do it like you said theres not really reason for an id right? BUT why the "photo" variable seems to be empty there? Its a global variable but still outside the function seems empty. Is this normal behavior? I am trying doing what you say but the variable is "undefined" when i try to append. –  Johny Jaz Jun 19 '13 at 15:30
    
... I don't know the facebook API. Is this function asynchronous? If so you need to move your render inside the call. –  Andrew Jun 19 '13 at 15:31
    
yes it is asynchronous! But if u put the append inside the else , then the photos work well as u suggested , BUT the AlbumName is out of scope in there so is undefined :p . haha i dont understand why they are out of scope though? By my programming experience(not js!) they should be in scope.. What am i missing here? –  Johny Jaz Jun 19 '13 at 15:40
    
Why make an array of the album names? Why not just set each one as a variable inside the loop? –  Andrew Jun 19 '13 at 15:42
    
the arrays are used just for the code to look more understandable to other people that will see it later. Dont mind that. Why is there a problem with that? In this particular case through we have a response inside a response. Because first i use the id of the page to get the album names. And the i use the id of every album to get their cover photo link. So we have one response inside another. So i guess is mandatory to have another variable name. –  Johny Jaz Jun 19 '13 at 15:44

As it was said id must be unique you should specify a class for each element instead, with jQuery it must looks like this :

$( ".coverPhoto" ).each(function( index ) {
       this.src = response.picture;
   );
});

Where coverPhoto is your class.

share|improve this answer

As stated by Dark Falcon, IDs MUST be unique.

Try something along these lines:

...
'<img src="nothing.jpg" id="coverPhoto'+i+'" />'+
...

And in your loading code:

(function(i) {
    FB.api( albumCover[i], function(response) {
        if(!response || response.error) {
            // render error
            alert("Noo!!");
        } else {
            // render photos
            document.getElementById('coverPhoto'+i).src = response.picture;
        }
    });
})(i);
share|improve this answer

You give the same ID to each <img>:

<img src= "nothing.jpg" id = "coverPhoto" />

ID must be unique

share|improve this answer
    
Yes you are absolutely right. I am trying to fix this now. Thank you very much for your answer –  Johny Jaz Jun 19 '13 at 15:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.