Hey friends and welcome to another project blogpost. Today we are going to see how to make this cool Dad Jokes Generator page using HTML, CSS & JS.


First, let's see the general usage of this project. Generally, a page with a preloaded joke is loaded and there is a button that says "Get a Dad Joke". When clicked, it loads a random dad joke.


Now let's see the source codes.


Paste the codes below in your HTML file:

<!-- Created by CodingPorium,2021. Visit https://youtube.com/c/CodingPorium for more tutorials with free source code -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dad Jokes Generator using JavaScript | CodingPorium</title>
</head>
<style>
 @import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap');    

*{
    padding:0;
    margin:0;
    box-sizing: border-box;
}

::selection{
    background-color: dodgerblue;
    color:white;
}

body{
    background-color: dodgerblue;
    color:black;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    font-family: poppins;
    font-size:10px;
}

.container{
    width:90%;
    max-width:500px;
    margin:0 auto;
    background-color: white;
    border-radius: 8px;
    padding:3rem;
    display: flex;
    flex-direction: column;
}

.container p{
    font-size:2rem;
    font-weight:300;
    line-height:3rem;
    text-align: justify;
}


 button {  
  margin-top: 40px;  
  width: fit-content;  
  padding: 2rem;  
  font-size: 2rem;  
  font-family: poppins;
  background-color: dodgerblue;  
  color: white;  
  border: none;  
  border-radius: 5px;  
  cursor: pointer;  
  align-self: flex-end;  
  transition: 0.3s;
 }  

 button:hover{
     opacity:0.7;
 }
</style>
<body>    
<div class="container">
    <div class="joke">
        <p></p>
    </div>
    <button>Get a Dad Joke</button>
</div>
</body>
<script>
    const button = document.querySelector(".container button");
    const jokeDiv = document.querySelector(".container .joke p");
    document.addEventListener("DOMContentLoaded", getJock);
    button.addEventListener("click", getJock);
    async function getJock(){
        const jokeData = await fetch("https://icanhazdadjoke.com/", {
            headers:{
                Accept:"application/json"
            }
        });
    const jokeObj = await jokeData.json();
    jokeDiv.innerHTML = jokeObj.joke;
    console.log(jokeData);
    }
</script>
</html>


And that's all for this project. Thanks for reading and do subscribe to our YouTube channel to support us.


Till the next one, goodbye!

Post a Comment

Previous Post Next Post