Issue Before:
When I run the loop using a console.log to display the data, it works. I get different headlines and it's corresponding snippets below. However when I include jQuery the headlines are repeated and the snippets do not display.
Updated Issue (includes thumbnail image and url links):
I am using the NY TIMES API and I am trying to write a loop that will list article;
Headlines
Snippets
Thumbnail Image
NY TIMES Url's that they are linked to
I tried different variations of the loops written for the article Headlines and their Snippets to get article Thumbnail Images and NY TIMES Url's but I am not getting any luck, there are also no errors being thrown in the console.
Here is a JSFiddle including the JSON API data:
https://jsfiddle.net/m15xg45j/
Below is my JS for this.
var url = "https://api.nytimes.com/svc/search/v2/articlesearch.json";
url += '?' + $.param({
'api-key' : "67dac4e994164fca920f7a8420e28dc2",
'q' : "health"
});
$.ajax({
url: url,
method: 'GET',
}).done(function(result){
var articles = result.response.docs;
for(var i=0; i<articles.length; i++){
$("h2").each(function(index, el){
$(el).html(articles[index].headline.main);
});
$("figcaption").each(function(index, el){
$(el).html(articles[index].snippet);
});
$("img").each(function(index, el){
$(el).html(articles[index].multimedia.url);
});
$("a").each(function(index, el){
$("href").attr(articles[index].web_url)
});
}
}).fail(function(err){
throw err;
});
and here is the HTML
<div>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg01.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg01.jpg" alt="">
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
</figcaption>
</figure>
</a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg02.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg02.jpg" alt="">
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, ut labore et dolore magna aliqua
</figcaption>
</figure>
</a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg03.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg03.jpg" alt="">
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, ut labore et dolore magna aliqua
</figcaption>
</figure>
</a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg04.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg04.jpg" alt="">
<figcaption>
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</figcaption>
</figure>
</a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg05.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg05.jpg" alt="">
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, ut labore et dolore magna aliqua
</figcaption>
</figure>
</a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg06.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg06.jpg" alt="">
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
</figcaption>
</figure>
</a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg07.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg07.jpg" alt="">
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, ut labore et dolore magna aliqua
</figcaption>
</figure>
</a>
<a href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg08.jpg">
<h2>Headline</h2>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/placeimg08.jpg" alt="">
<figcaption>
Lorem ipsum dolor sit amet, consectetur adipiscing elit
</figcaption>
</figure>
</a>
</div>
$("h2").html(articles[i].headline.main);will replace the contents of all currently existingh2elements with the contents ofarticles[i].headline.main. Same for the next line with the figcaption. If those elements don't exist yet, you won't get any output. I think you want to append the data to something, or create new elements, instead.