This question already has an answer here:
I'm having a very hard time figuring this out. I'm working on a NewsAPI project. Basically I pull out 6 articles, I store the title of the articles in an array, the descriptions in another, and the sources in another one too.
The sources are hidden on purpose in the browser, but what I am trying to do is to look at the source of a specific article (let's say CNN) when the user clicks on that article, and then store the source he clicked on in another array. I have tried to use a if statement, I had a lot of syntax errors, but I believe I fixed those, and I am not sure why this is not working.
Here is the whole code, it's a little dense as there are others stuff happening in the code besides this source issue. Basically what it should do is log the source of the article in the console on click of the respective article. Right now it only logs the source of the last article [5] in the array, and I don't understand why...
Thanks for your help guys!
var input = document.getElementById('input');
var container = document.getElementById('container');
var allTitles = [];
var allDescription = [];
var allNumbers = [];
var allSource = [];
var allSource2 = [];
var articleSourceArray = [];
var i =0;
function getNews(event) {
if (event.keyCode == 13) {
var everything = $.get("https://newsapi.org/v2/top-headlines?q=" + input.value + "&language=en&apiKey=612c24355bc24dbcbb4b13496d772971");
everything.done(function(data) {
for (var i = 0; i < 6; i++) {
//DEFINE ALL THE VARIABLES
var articleSource = document.createElement('p');
var articleSourceContainer = document.createElement('DIV');
var articleContainer = document.createElement('DIV');
var titleContainer = document.createElement('DIV');
var articleTitle = document.createElement('a');
var articleDescription = document.createElement('p');
articleSource.classList.add("article-source");
titleContainer.classList.add("title-container");
articleTitle.classList.add('article-title');
articleContainer.classList.add('article-container');
articleDescription.classList.add('article-description');
articleSource.setAttribute('value',"0");
articleContainer.appendChild(articleSource);
container.appendChild(articleContainer);
articleContainer.appendChild(titleContainer);
titleContainer.appendChild(articleTitle);
titleContainer.appendChild(articleDescription);
articleSource.innerHTML = data.articles[i].source.name;
articleTitle.innerHTML = data.articles[i].title;
articleDescription.innerHTML = data.articles[i].description;
allTitles.push(data.articles[i].title);
allDescription.push(data.articles[i].description);
allSource.push(data.articles[i].source.name);
document.getElementsByClassName('article-container')[i]
.addEventListener('click', function (event) {
// do something
console.log(articleTitle);
if (articleSource.title == 'CNN')
console.log(articleSource);
// allSource2.push(articleSource.innerHTML);
});
}
});
input.value = "";
while (container.firstChild) {
container.removeChild(container.firstChild);
// (container2.firstChild)
// container2.removeChild(container2.firstChild);
allTitles.length = 0;
allDescription.length = 0;
allSource.length = 0;
}
}
}
input.addEventListener('keyup', getNews);
Aucun commentaire:
Enregistrer un commentaire