-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavascript8.js
More file actions
54 lines (46 loc) · 1.8 KB
/
Copy pathJavascript8.js
File metadata and controls
54 lines (46 loc) · 1.8 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
const API_KEY = '965434254f0fc23138086df2565f7f4b'
const APILINK = `https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=${API_KEY}&page=1`;
const IMG_PATH = `https://image.tmdb.org/t/p/w1280`;
const main = document.getElementById("section");
const form = document.getElementById("form");
const search = document.getElementById("query");
const SEARCHAPI = `https://api.themoviedb.org/3/search/movie?&api_key=${API_KEY}&query=`;
returnMovies(APILINK);
function returnMovies(url) {
fetch(url)
.then(res=>res.json())
.then(function(data){
console.log(data);
console.log(data.results);
data.results.forEach(element => {
const div_card = document.createElement('div');
const div_row = document.createElement('div');
const div_column = document.createElement('div');
const image = document.createElement('img');
const title = document.createElement('h3');
title.innerHTML = `${element.title}`;
image.src = IMG_PATH + element.poster_path;
image.alt = data.title
div_card.appendChild(image);
div_card.appendChild(title);
div_column.appendChild(div_card);
div_row.appendChild(div_column);
div_card.setAttribute('class', 'card');
div_row.setAttribute('class', 'row');
div_column.setAttribute('class', 'column');
main.appendChild(div_row);
});
});
}
form.addEventListener("submit", (e) => {
e.preventDefault();
main.innerHTML = "";
const searchItem = search.value;
if (searchItem) {
console.log(`Searching for "${searchItem}"`);
returnMovies(SEARCHAPI + searchItem);
} else {
returnMovies(APILINK);
console.log("No Change");
}
})