Created
September 4, 2017 23:49
-
-
Save anyweez/458ead96610dd925325ae1d92ac478c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// DOM elements | |
const searchBox = document.querySelector('#movie-search'); // search box | |
const searchBtn = document.querySelector('#go-btn'); // search button | |
const resultsBox = document.querySelector('ul'); // search results | |
// When the search button is clicked, run this function. | |
searchBtn.addEventListener('click', function () { | |
const searchText = searchBox.value; | |
searchFor(searchText); | |
}); | |
// This function searches for the specified text and renders the results. | |
function searchFor(text) { | |
fetch(`http://custom-api.com/api/movies?name=${text}`) | |
.then(function (response) { | |
return response.json(); | |
}) | |
.then(function (body) { | |
// Get rid of all DOM elements before adding new ones. | |
resultsBox.innerHTML = ''; | |
// Assume that 'body' is an object with a 'movies' property (an array) | |
for (let i = 0; i < body.movies.length; i++) { | |
const html = `<li>Movie name: ${body.movies[i].name}</li>`; | |
resultsBox.innerHTML += html; | |
} | |
}); | |
} |
This file contains hidden or 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
<!doctype html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<h1>Popular Movies</h1> | |
<input type="text" id="movie-search"> | |
<button id="go-btn">Go</button> | |
<ul> | |
</ul> | |
<script src="app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment