JAVASCRIPT   14

randog.js

Guest on 26th July 2021 04:08:53 PM

  1. var btn = document.querySelector("#btn");
  2. var img = document.querySelector("#photo");
  3.  
  4. //listen for clicks
  5. btn.addEventListener("click", function(){
  6.     //make the request
  7.     var XHR = new XMLHttpRequest();
  8.     XHR.onreadystatechange = function(){
  9.         if(XHR.readyState == 4 && XHR.status == 200){
  10.             var url = JSON.parse(XHR.responseText).message;
  11.             img.src = url;
  12.  
  13.         }
  14.     }
  15.     XHR.open("GET", "https://dog.ceo/api/breeds/image/random");
  16.     XHR.send();
  17. });

Raw Paste


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