0

I'm trying to integrate two jQuery scripts into one but because the elements of the first part are created dynamically using jQuery I can't get the second part of the project working.

The project:

1.Instagram pictures are parsed as JSON into li elements.

Here's a portion of the code:

<ul id="elasticstack" class="elasticstack">
</ul>
<script>
$("#elasticstack").append("<li><div class='peri-pic'><img class='instagram-image' src='" + data.data[i].images.low_resolution.url +"' /><span class='name'>"+ data.data[i].caption.from.username +"</span> <span class='likes'><span class='glyphicon glyphicon-heart'></span><p>"+data.data[i].likes.count +"</p></span></div></li>"
 ); 
</script>

Source: LINK

2.This works fine but when I try to add the slider none of the functions work. Even if I wrap the callout function in a $( document ).ready(function() { });

Here's the call out code:

<script>
 new ElastiStack( document.getElementById( 'elasticstack' ) );
</script>

Source: LINK

Here's the JS Fiddle with all my code: LINK

Where am I going wrong?

asked Sep 1, 2014 at 22:55
7
  • 1
    Don't create the ElastiStack until the images have been appended to the DOM. Commented Sep 1, 2014 at 23:05
  • @Mathletics how do I do that? Commented Sep 1, 2014 at 23:08
  • 1
    Hm. I don't think it is good idea mixing pure js with jQuery Commented Sep 1, 2014 at 23:08
  • @bksi that's a very good point. I imagined there'd be some sort of conflict. I've never mixed them before. Commented Sep 1, 2014 at 23:09
  • did you try to use $("#elasticstack") instead document.getElementById...? Commented Sep 1, 2014 at 23:12

3 Answers 3

1

You're looking for this. After the initial loadImages(start_url) call, you should be able to call loadImages(next_url) to load and display more. new ElastiStack had to be called after the images had been appended.

var access_token = "18360510.5b9e1e6.de870cc4d5344ffeaae178542029e98b",
 user_id = "18360510", //userid
 start_url = "https://api.instagram.com/v1/users/"+user_id+"/media/recent/?access_token="+access_token,
 next_url;
function loadImages(url){
 $.ajax({
 type: "GET",
 dataType: "jsonp",
 cache: false,
 url: url,
 success: function(data){
 displayImages(data);
 next_url = data.pagination.next_url;
 }
 })
}
function displayImages(images){
 for(var i = 0; i < 20; i++){
 if(images.data[i]){
 $("#elasticstack").append("<li><img class='instagram-image' src='" + images.data[i].images.low_resolution.url + "'></li>");
 }
 }
 // Call it after the images have been appended
 new ElastiStack(document.getElementById('elasticstack'));
}
$(document).ready(function(){
 loadImages(start_url);
});
answered Sep 1, 2014 at 23:16
Sign up to request clarification or add additional context in comments.

Comments

1

Try to initialize the ElastiStack after data (HTML elements) has been appended into the DOM in your ajax, for example:

for (var i = 0; i < count; i++) {
 // ...
 $("#elasticstack").append(...);
}
new ElastiStack(...);

It should work.

answered Sep 1, 2014 at 23:15

Comments

1
 $.ajax({
 type: "GET",
 dataType: "jsonp",
 cache: false,
 url: url ,
 success: function(data) {
 next_url = data.pagination.next_url;
 //count = data.data.length;
 //three rows of four
 count = 20; 
 //uncommment to see da codez
 //console.log("count: " + count );
 //console.log("next_url: " + next_url );
 //console.log("data: " + JSON.stringify(data) );
 for (var i = 0; i < count; i++) {
 if (typeof data.data[i] !== 'undefined' ) {
 //console.log("id: " + data.data[i].id);
 $("#elasticstack").append("<li><img class='instagram-image' src='" + data.data[i].images.low_resolution.url +"' /></li>"
 ); 
 } 
 } 
 new ElastiStack(document.getElementById('elasticstack'));
 }
 });

You have to move your new ElastiStack(document.getElementById('elasticstack')); inside the ajax success event. You don't have to change anything else in your. I also updated your JSFiddle.

answered Sep 1, 2014 at 23:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.