Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "Random Joke Generator" project using HTML, CSS & JavaScript.
Preview
In the above video, you’ve seen the preview of the "Random Joke Generator" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS and JavaScript code below.
Random Joke Generator [Source Code]
To get the following HTML, CSS & JS code for the Random Joke Generator project. You need to create three files one is a HTML file, second one is a CSS file and the another one is JS file. After creating these three files then you can copy-paste the given codes on your document.
Remember, you’ve to create a file with .html extension for HTML code, .css extension for CSS code and .js for JavaScript code.
You can also download all source code files from the given download button.
<!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>Random Joke Generator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Random Joke Generator</h1>
<img src="emoji.png" alt="">
<p class="joke fade">Click on the "GENERATE" to get the Joke.</p>
<button class="btn">Generate</button>
</div>
<script src="index.js"></script>
</body>
</html>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background-color: yellow;
}
.container {
width: 500px;
min-height: 200px;
padding: 20px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
background-color: yellowgreen;
border-radius: 20px;
box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.6);
}
img {
width: 150px;
height: 150px;
}
.joke {
text-align: center;
color: black;
font-weight: 500;
opacity: 0;
}
.fade {
opacity: 1;
transition: opacity 1.5s;
}
button {
padding: 10px 25px;
margin-top: 20px;
font-weight: bold;
box-shadow: 5px 5px 0;
cursor: pointer;
position: relative;
}
button:active {
position: relative;
top: 5px;
left: 5px;
box-shadow: none;
}
const btnEl = document.querySelector(".btn");
const jokeEl = document.querySelector(".joke");
var URL = "https://v2.jokeapi.dev/joke/Any?type=single&amount=1";
btnEl.addEventListener("click",getMethod);
async function getMethod(){
jokeEl.classList.remove("fade");
const data = await fetch(URL).then((e)=>
e.json());
if(data){
console.log(data);
jokeEl.innerHTML = data.joke;
jokeEl.classList.add("fade");
}
}
