-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
73 lines (45 loc) · 1.63 KB
/
main.js
File metadata and controls
73 lines (45 loc) · 1.63 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
document.querySelector(".js-go").addEventListener('click', function() {
var inputValue = document.querySelector('.js-userinput').value;
var userInput = getUserInput();
searchGiphy( userInput );
});
document.querySelector('.js-userinput').addEventListener('keyup', function (e) {
if (e.which === 13) {
var userInput = getUserInput();
searchGiphy( userInput );
}
});
function getUserInput() {
var inputValue = document.querySelector('.js-userinput').value;
return inputValue;
}
function searchGiphy( searchQuery ) {
var url = "https://api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&q=" + searchQuery;
// AJAX Request
var GiphyAJAXCall = new XMLHttpRequest();
GiphyAJAXCall.open( 'GET', url );
GiphyAJAXCall.send();
GiphyAJAXCall.addEventListener('load', function( data ) {
var actualData = data.target.response;
pushToDOM(actualData);
console.log(actualData);
});
}
function pushToDOM( response ) {
// turn response into real javascript object
response = JSON.parse( response );
// drill down to the data array
var images = response.data;
// find the container to hold this stuff in DOM
var container = document.querySelector('.js-container');
// clear it of old content since this function will be used on every search
// we want to reset the div
container.innerHTML = "";
// loop through data array and add IMG html
images.forEach(function(image){
// find img src
var src = image.images.fixed_height.url;
// concatenate a new IMG tag
container.innerHTML += "<img src='"+ src +"' class='container-image' />";
});
}