JAVASCRIPT API ISSUE


18
views
0
9 weeks ago by
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
//create API REQUEST
var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){

//CHECKING STATUS CODE
if(this.readyState == 4 && this.status == 200){

//PARSING JSON DATA
var data = JSON.parse(this.responseText);

//LOOPING THROUGH DATA
for(var key in data){

//add the looped items to variable
var items = data[key];

var tablerow = document.createElement("tr");
var tabledetail1 = document.createElement("td");
var tabledetail2 = document.createElement("td");
var tabledetail3 = document.createElement("td");

// add CSS id to tabe row
tablerow.id = 'tablerow';

// add CSS id to tabledetail
tabledetail1.id='tabledetail1';

//get image url from object
var imgurl = items.image.small;

//create image element
var img = document.createElement('img');

//inject url into img src
img.src = imgurl;

//add id for styling
img.id ='img';

//add detail to row
tablerow.appendChild(tabledetail1);
tablerow.appendChild(tabledetail2);
tablerow.appendChild(tabledetail3);

//add img to table detail
tabledetail1.appendChild(img);

//get all coin id's
var id = document.createTextNode(items.id);

//append id to table detail
tabledetail2.appendChild(id);

//get price
var price = document.createTextNode(items.market_data.current_price.btc);

//apply price to tableheading1
tabledetail3.appendChild(price);

//append table row to table in document
document.getElementById('box').appendChild(tablerow);

}

}

}

//DEFINING GET REQUEST WITH URL
xhr.open('GET','https://api.coingecko.com/api/v3/coins?per_page=1977');
//SEND REQUEST
xhr.send();

</script>
<table id='box'></table>
</body>
</html>





/* THIS IS THE CODE THAT IS SUPPOSE TO RETRIEVE ALL 250 IMAGES FROM THE
COINS VIA THE XMLHttpRequest but i am only getting from 0-99 images only
for some reason all the images seem to be there when i inspect the
response in the console but cant seem to access all of the images with
the 'for in' loop */
Community: javascript
Please login to add an answer/comment or follow this question.

Similar posts:
Search »
  • Nothing matches yet.