|
| 1 | +// Let's make a randomized dog photo gallery! |
| 2 | + |
| 3 | +// Write a function that makes an API call to https://dog.ceo/api/breeds/image/random. It should trigger after clicking a |
| 4 | +// button in your webpage.Every time the button is clicked it should append a new dog image to the DOM. |
| 5 | + |
| 6 | +// Create an index.html file that will display your random image |
| 7 | +// Add 2 < button > and 1 < ul > element, either in the HTML or through JavaScript |
| 8 | +// Write two versions for the button functionality: one with XMLHttpRequest, and the other with axios |
| 9 | +// When any one of the 2 buttons is clicked it should make an API call to https://dog.ceo/api/breeds/image/random |
| 10 | +// After receiving the data, append to the < ul > a < li > that contains an < img > element with the dog image |
| 11 | +// Incorporate error handling |
| 12 | + |
| 13 | +let btn = document.getElementById("showImgBttn").addEventListener('click', loadImg); |
| 14 | + |
| 15 | +function loadImg() { |
| 16 | + |
| 17 | + var xhttp = new XMLHttpRequest(); |
| 18 | + xhttp.onreadystatechange = function () { |
| 19 | + if (this.readyState == 4 && this.status == 200) { |
| 20 | + let url = JSON.parse(xhttp.responseText); |
| 21 | + console.log(url.message); |
| 22 | + document.getElementById("randomPic").src = url.message; |
| 23 | + } |
| 24 | + }; |
| 25 | + xhttp.open("GEt", "https://dog.ceo/api/breeds/image/random", true); |
| 26 | + xhttp.send(); |
| 27 | +} |
| 28 | + |
| 29 | +let getImg = () => { |
| 30 | + axios.get("https://dog.ceo/api/breeds/image/random").then(response => { |
| 31 | + console.log(response.data.message); |
| 32 | + document.getElementById("randomPicAxios").src = response.data.message; |
| 33 | + }); |
| 34 | +}; |
| 35 | + |
| 36 | + |
| 37 | +let btn2 = document.getElementById("showImgAxios").addEventListener("click", getImg); |
0 commit comments