Step-by-Step Guide: Building a Random Quotes Generator in JavaScript!
Education
Introduction
Welcome back! In this article, we will create a simple and functional Random Quotes Generator using only JavaScript. This guide will take you through the process step-by-step, from HTML structure to JavaScript functionality. Let's get started!
Introduction
In today’s tutorial, we will create a random quotes generator that showcases a new quote every time you click a button. We will utilize HTML for the structure, CSS for styling, and JavaScript for functionality.
1. Setting Up the HTML Structure
To start, we need to create a basic HTML layout. Below is a sample structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css"> (* Link to your CSS *)
<title>Random Quotes Generator</title>
</head>
<body>
<div class="container">
<div id="quote-box">
<p class="quote-heading">"Your initial quote will be displayed here."</p>
<p class="source">- Author Name</p>
<span class="citation-year">Citation and Year</span>
</div>
<button id="load-quote">Next Quote</button>
</div>
<script src="script.js"></script> (* Link to your JavaScript *)
</body>
</html>
This structure consists of a container that holds the quote and a button to generate the next quote.
2. JavaScript Functionality
Next, we will implement the logic for generating the quotes using JavaScript. You will need to create a JavaScript file, script.js
, and include the following code:
const quotes = [
( quote: "The best way to predict your future is to create it.", source: "Peter Drucker", citation: "Leadership", year: 2000 ),
( quote: "Life is 10% what happens to us and 90% how we react to it.", source: "Charles R. Swindoll", citation: "Motivation", year: 1999 ),
( quote: "The only way to do great work is to love what you do.", source: "Steve Jobs" ),
// Add more quotes here
];
function getRandomQuote() (
const randomIndex = Math.floor(Math.random() * quotes.length);
return quotes[randomIndex];
)
function printQuote() (
const randomQuote = getRandomQuote();
let quoteString = `<p class="quote-heading">${randomQuote.quote)</p>`;
quoteString += `<p class="source">- $(randomQuote.source)</p>`;
if (randomQuote.citation) (
quoteString += `<span class="citation">${randomQuote.citation)</span>`;
}
if (randomQuote.year) (
quoteString += `<span class="year">${randomQuote.year)</span>`;
}
document.getElementById('quote-box').innerHTML = quoteString;
}
document.getElementById('load-quote').addEventListener('click', printQuote);
// Optional: Auto-generate a new quote every 15 seconds
setInterval(printQuote, 15000);
Breakdown of the JavaScript Code
Quotes Array: It stores multiple quote objects that include the quote text, source, citation, and year.
getRandomQuote Function: This function generates a random index and retrieves a corresponding quote object from the quotes array.
printQuote Function: This function constructs the HTML for the selected quote and inserts it into the quote box.
Event Listener: The button is set up to call the
printQuote
function each time it's clicked, displaying a new quote.Auto-Generation: Optionally, you can set a timer to change quotes automatically every 15 seconds.
Conclusion
Congratulations! You have successfully built a random quotes generator using JavaScript. You can further enhance it by adding more quotes, styling the layout with CSS, or even integrating it into a larger project.
Keywords
- JavaScript
- HTML
- Quotes Generator
- Random Quotes
- Web Development
FAQ
Q: What technologies are used in this project?
A: This project uses HTML for structure, CSS for styling, and JavaScript for functionality.
Q: Can I add more quotes to the generator?
A: Yes! You can simply add more quote objects to the quotes
array in the script.js
.
Q: Does the generator automatically change quotes?
A: Yes, you can set an interval to automatically generate new quotes at 15 seconds in the script.
Q: How do I style the quotes generator?
A: You can create a styles.css
file to customize the appearance of the quotes generator.
Feel free to explore more features and make this project unique to showcase your creativity! Happy coding!