-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
44 lines (39 loc) · 1.07 KB
/
script.js
File metadata and controls
44 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const apiKey = 'tTPoLoXghRzZIb0h8xoAhA==3JZX6HuYYpo6BUr0';
const apiUrl = `https://api.api-ninjas.com/v1/quotes`;
let authorName=document.querySelector('.author')
let tag = document.querySelector('.tag')
let quote=document.getElementsByTagName('q')
const generateBtn=document.querySelector('.generate-btn')
const copyBtn=document.querySelector('.copy-btn')
let featchData=async()=>{
await fetch(apiUrl, {
headers: {
'X-Api-Key': apiKey
}
})
.then(response => {
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(data);
authorName.textContent=data[0].author
quote.item(0).textContent=data[0].quote
tag.innerHTML=data[0].category
})
.catch(error => {
console.error('Error:', error);
});
}
featchData()
let generateReQuote=()=>{
featchData();
}
let copyQuote=async()=>{
await navigator.clipboard.writeText(quote.textContent)
alert('Quote copied ')
}
generateBtn.addEventListener('click', generateReQuote)
copyBtn.addEventListener('click', copyQuote)