Random Quote on Your Webpage with JavaScript
Surprise me with cute awesome photo
Make an HTTP request in Javascript?
Team card with tailwind css
Tutorial React Best practices
Give me 3 ideas about how to plan good New Years resolutions. Give me some that are personal ,family, and professionally-oriented.
Resignation Letter Assistance
write me short joke for web development tutorial starting
Sure, here's a short joke for a web development tutorial starting:
Why was the web developer always cold?
Because he left his Windows open!
Display a Random Quote on Your Webpage with JavaScript
To display a random quote on your webpage using JavaScript, you can follow these steps:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Quote Generator</title>
</head>
<body>
<div id="quote-container">
<p id="quote-text"></p>
</div>
<button id="new-quote-button">New Quote</button>
<script src="script.js"></script>
</body>
</html>
const quotes = [
{
text: "The only way to do great work is to love what you do.",
author: "Steve Jobs"
},
{
text: "Innovation distinguishes between a leader and a follower.",
author: "Steve Jobs"
},
{
text: "Your time is limited, don't waste it living someone else's life.",
author: "Steve Jobs"
},
// Add more quotes here
];
const quoteContainer = document.getElementById("quote-container");
const quoteText = document.getElementById("quote-text");
const newQuoteButton = document.getElementById("new-quote-button");
// Function to generate and display a random quote
function displayRandomQuote() {
const randomIndex = Math.floor(Math.random() * quotes.length);
const randomQuote = quotes[randomIndex];
quoteText.textContent = randomQuote.text - randomQuote.author;
}
// Initial display of a random quote
displayRandomQuote();
// Event listener for the "New Quote" button
newQuoteButton.addEventListener("click", displayRandomQuote);
This code selects a random quote from the `quotes` array when the "New Quote" button is clicked and displays it in the `quote-container` element.
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f4f4f4;
}
#quote-container {
margin: 2rem auto;
padding: 2rem;
max-width: 600px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
#quote-text {
font-size: 1.5rem;
font-style: italic;
margin-bottom: 1rem;
}
#new-quote-button {
padding: 0.5rem 1rem;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
#new-quote-button:hover {
background-color: #0056b3;
}
This example demonstrates a simple random quote generator using HTML, CSS, and JavaScript. You can expand and customize it by adding more quotes, additional styling, or even by fetching quotes from an external API for a more dynamic experience.
/imagine Surprise me with random cute awesome photo