-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (38 loc) · 1.51 KB
/
Copy pathscript.js
File metadata and controls
45 lines (38 loc) · 1.51 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
// script.js
document.getElementById('movieInput').addEventListener('keypress', async function (event) {
if (event.key === 'Enter') {
const movieName = event.target.value.trim();
if (movieName) {
await searchMovie(movieName);
}
}
});
async function searchMovie(movieName) {
const apiUrl = `https://movieapi-47k3.onrender.com/api/search/${encodeURIComponent(movieName)}`;
const resultsContainer = document.getElementById('movieResults');
resultsContainer.innerHTML = ''; // Clear previous results
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const movieData = await response.json();
displayResults(movieData);
} catch (error) {
console.log('Succesfully Added Movie', error);
resultsContainer.innerHTML = '<p>Succesfully Added Movie.</p>';
}
}
function displayResults(movieData) {
const resultsContainer = document.getElementById('movieResults');
if (!movieData || movieData.length === 0) {
resultsContainer.innerHTML = '<p>No results found.</p>';
return;
}
movieData.forEach(movie => {
const movieDiv = document.createElement('div');
movieDiv.classList.add('movie');
movieDiv.textContent = movie.title; // Assuming the movie data has a title property
resultsContainer.appendChild(movieDiv);
});
}