|
1 |
| -/* 1. Grab the input value */ |
2 |
| - |
3 |
| - |
4 |
| -document.querySelector(".js-go").addEventListener('click',function(){ |
5 |
| - |
6 |
| - var input = document.querySelector("input").value; |
7 |
| - pushToDOM(input); |
8 |
| - |
9 |
| -}); |
10 |
| - |
11 |
| -document.querySelector(".js-userinput").addEventListener('keyup',function(e){ |
12 |
| - |
13 |
| - var input = document.querySelector("input").value; |
14 |
| - |
15 |
| - // if the key ENTER is pressed... |
16 |
| - if(e.which === 13) { |
17 |
| - pushToDOM(input); |
18 |
| - } |
19 |
| - |
| 1 | +document.querySelector(".js-go").addEventListener("click", function () { |
| 2 | + var inputValue = document.querySelector(".js-userinput").value; |
| 3 | + var userInput = getUserInput(); |
| 4 | + searchGiphy(userInput); |
20 | 5 | });
|
21 | 6 |
|
22 |
| -/* 2. do the data stuff with the API */ |
23 |
| - |
24 |
| -var url = "http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC"; |
25 |
| - |
26 |
| -// AJAX Request |
27 |
| -var GiphyAJAXCall = new XMLHttpRequest(); |
28 |
| -GiphyAJAXCall.open( 'GET', url ); |
29 |
| -GiphyAJAXCall.send(); |
30 |
| - |
31 |
| -GiphyAJAXCall.addEventListener('load',function(e){ |
32 |
| - |
33 |
| - var data = e.target.response; |
34 |
| - pushToDOM(data); |
35 |
| - |
36 |
| -}); |
37 |
| - |
38 |
| - |
39 |
| - |
40 |
| - |
41 |
| -/* 3. Show me the GIFs */ |
42 |
| - |
43 |
| - |
44 |
| -function pushToDOM(input) { |
| 7 | +document |
| 8 | + .querySelector(".js-userinput").addEventListener("keyup", function (data) { |
| 9 | + if (data.which === 13) { |
| 10 | + var userInput = getUserInput(); |
| 11 | + searchGiphy(userInput); |
| 12 | + } |
| 13 | + }); |
45 | 14 |
|
46 |
| - var response = JSON.parse(input); |
| 15 | +function getUserInput() { |
| 16 | + var inputValue = document.querySelector(".js-userinput").value; |
47 | 17 |
|
48 |
| - var imageUrls = response.data; |
| 18 | + return inputValue; |
| 19 | +} |
49 | 20 |
|
50 |
| - imageUrls.forEach(function(image){ |
| 21 | +function searchGiphy(searchQuery) { |
| 22 | + var url = |
| 23 | + "https://api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&q=" + searchQuery; |
| 24 | + // AJAX Request |
| 25 | + var GiphyAJAXCall = new XMLHttpRequest(); |
| 26 | + GiphyAJAXCall.open("GET", url); |
| 27 | + GiphyAJAXCall.send(); |
| 28 | + |
| 29 | + GiphyAJAXCall.addEventListener("load", function (data) { |
| 30 | + var actualData = data.target.response; |
| 31 | + pushToDOM(actualData); |
| 32 | + console.log(actualData); |
| 33 | + }); |
| 34 | +} |
51 | 35 |
|
| 36 | +function pushToDOM(response) { |
| 37 | + // turn response into real javascript object |
| 38 | + response = JSON.parse(response); |
| 39 | + // drill down to the data array |
| 40 | + var images = response.data; |
| 41 | + |
| 42 | + // find the container to hold this stuff in DOM |
| 43 | + var container = document.querySelector(".js-container"); |
| 44 | + // clear it of old content since this function will be used on every search |
| 45 | + // we want to reset the div |
| 46 | + container.innerHTML = ""; |
| 47 | + |
| 48 | + // loop through data array and add IMG html |
| 49 | + images.forEach(function (image) { |
| 50 | + // find img src |
52 | 51 | var src = image.images.fixed_height.url;
|
53 |
| - console.log(src); |
54 |
| - |
55 |
| - var container = document.querySelector(".js-container"); |
56 |
| - container.innerHTML += "<img src=\"" + src + "\" class=\"container-image\">"; |
57 | 52 |
|
| 53 | + // concatenate a new IMG tag |
| 54 | + container.innerHTML += "<img src='" + src + "' class='container-image' />"; |
58 | 55 | });
|
59 |
| - |
60 | 56 | }
|
0 commit comments