-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (54 loc) · 1.75 KB
/
Copy pathscript.js
File metadata and controls
74 lines (54 loc) · 1.75 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const container = document.getElementById('container')
const loading = document.getElementById('loading')
const btn = document.getElementById('btn')
const search = document.getElementById('search')
const toggle = document.getElementById('toggle')
const body = document.body
function fetchAnime(query) {
if (!query) return
loading.style.display = 'block'
loading.innerText = "Loading..."
container.innerHTML = ''
fetch(`https://api.jikan.moe/v4/anime?q=${query}`)
.then(res => res.json())
.then(data => {
loading.style.display = 'none'
data.data.forEach(item => {
const card = document.createElement('div')
card.className = 'card'
const img = document.createElement('img')
img.src = item.images.jpg.image_url
const title = document.createElement('h3')
title.innerText = item.title
const score = document.createElement('p')
score.innerText = "⭐ " + item.score
card.appendChild(img)
card.appendChild(title)
card.appendChild(score)
container.appendChild(card)
})
})
.catch(() => {
loading.innerText = "Failed to load data"
})
}
btn.addEventListener('click', () => {
fetchAnime(search.value)
})
search.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
fetchAnime(search.value)
}
})
toggle.addEventListener('click', () => {
if (body.classList.contains('light')) {
body.classList.remove('light')
body.classList.add('dark')
toggle.innerText = '☀️'
} else {
body.classList.remove('dark')
body.classList.add('light')
toggle.innerText = '🌙'
}
})
fetchAnime("naruto")