-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81a5cad
commit 8ff4c56
Showing
2 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// script.js | ||
|
||
// API endpoint for Quotable API | ||
const apiUrl = "https://api.quotable.io/random"; | ||
|
||
// Function to fetch a random quote | ||
function getQuote() { | ||
fetch(apiUrl) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
const quote = data.content; | ||
const author = data.author; | ||
displayQuote(quote, author); | ||
}) | ||
.catch((error) => console.error("Error fetching quote:", error)); | ||
} | ||
|
||
// Function to display the quote | ||
function displayQuote(quote, author) { | ||
const quoteElement = document.querySelector(".quote"); | ||
const authorElement = document.querySelector(".author"); | ||
quoteElement.textContent = `"${quote}"`; | ||
authorElement.textContent = `—${author}`; | ||
} | ||
|
||
// Add event listener to generate button | ||
document.getElementById("generate-btn").addEventListener("click", getQuote); | ||
|
||
// Fetch a quote on page load | ||
getQuote(); | ||
|
||
// Copy Quote | ||
|
||
const quote = document.getElementById('quote'); | ||
const CopyBtn = document.getElementById('copy-btn'); | ||
|
||
CopyBtn.addEventListener("click", async () => { | ||
try { | ||
await navigator.clipboard.writeText(quote.textContent); | ||
alert("Quote copied to clipboard!"); | ||
} catch (error) { | ||
console.error("Error copying quote:", error); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters