Skip to content

Commit fe25446

Browse files
authored
Update main.js
1 parent f2d9741 commit fe25446

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

GiphySearch/javascript/main.js

+53-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,60 @@
1+
/* 1. Grab the input value */
12

23

34
document.querySelector(".js-go").addEventListener('click',function(){
4-
var input = document.querySelector("input").value;
5-
pushToDom(input);
65

6+
var input = document.querySelector("input").value;
7+
pushToDOM(input);
78

89
});
910

10-
function pushToDom(input){
11-
var container= document.querySelector(".js-container");
12-
container.innerHTML=input;
13-
}
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+
20+
});
21+
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) {
45+
46+
var response = JSON.parse(input);
47+
48+
var imageUrls = response.data;
49+
50+
imageUrls.forEach(function(image){
51+
52+
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+
58+
});
59+
60+
}

0 commit comments

Comments
 (0)