Dog picture API

 HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dog API</title>
</head>
<body>
    <h1>Show Random Dog images</h1>
    <button>show image</button>
    <img id="result">
    <script src="https://cdn.jsdelivr.net/npm/axios@1.6.7/dist/axios.min.js"></script>
    <script src="DogAPI.js"></script>
</body>
</html>

CSS


let url="https://dog.ceo/api/breeds/image/random";;
let btn=document.querySelector("button");

btn.addEventListener("click",async ()=>{
    let link=await getImg();
    console.log(link);
    let img=document.querySelector("#result");
     img.setAttribute("src",link);
});

async function getImg() {
    try{
        let res=await axios.get(url);
        return res.data.message;
    }
   
catch(e){
    console.log(e);
    return "No img found";
}
}

Comments

Popular posts from this blog

Callback Hell

TODO list with DOM

Refactoring callback hell code