JAVASCRIPT   15

bitcoin.js

Guest on 26th July 2021 04:09:52 PM

  1. var btn = document.querySelector("button");
  2. var cash = document.querySelector(".price");
  3.  
  4. /* XMLHttpRequest
  5.  
  6. //adding event to button
  7. btn.addEventListener("click", function(){
  8.  
  9. //new request
  10. var XHR = new XMLHttpRequest();
  11.  
  12. //checks when the status changes
  13. XHR.onreadystatechange = function(){
  14.     if(XHR.readyState == 4 && XHR.status == 200){
  15.         var data = JSON.parse(XHR.responseText);
  16.         var money = data.bpi.GBP.rate;
  17.         cash.innerText = money;
  18.     }
  19. }
  20. XHR.open("GET",  "https://api.coindesk.com/v1/bpi/currentprice.json");
  21. XHR.send();
  22. })
  23.  
  24. */
  25. btn.addEventListener("click",function(){
  26.  
  27.     var url = "https://api.coindesk.com/v1/bpi/currentprice.json";
  28.     fetch(url)
  29.     .then(function(res){
  30.         console.log(res);
  31.         return res.json()
  32.     })
  33.     .then(function(data){
  34.         console.log(data.bpi.USD.rate)
  35.         var price = data.bpi.USD.rate;
  36.         cash.innerText = price;
  37.     })
  38.     });

Raw Paste


Login or Register to edit or fork this paste. It's free.