-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
94 lines (92 loc) · 3.03 KB
/
index.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ZipCode App</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.0/css/bulma.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<style>
.icon-remove,.icon-check{
display: none;
}
</style>
</head>
<body>
<section class="hero hero-body is-dark container">
<h1 class="title">US Location Info </h1>
<h2 class="subtitle">Enter a zipcode to get the location
info</h2>
<form action="" id="zipForm">
<div class="field">
<div class="control has-icons-left has-icons-right">
<input class="input zip is-large" type="text" placeholder="ZipCode" >
<span class="icon is-small is-left">
<i class="fa fa-map-pin"></i>
</span>
<span class="icon is-small is-right icon-check">
<i class="fa fa-check"></i>
</span>
<span class="icon is-small is-right icon-remove">
<i class="fa fa-remove"></i>
</span>
</div>
</div>
</form>
</section>
<br>
<div class="container">
<div class="output">
</div>
</div>
<script type="text/javascript">
document.querySelector('#zipForm').addEventListener('submit',getLocationInfo);
document.querySelector('body').addEventListener('click',deleteLocation);
function getLocationInfo(e){
const zip=document.querySelector('.zip').value;
fetch(`http://api.zippopotam.us/us/${zip}`).then(response=>{
if(response.status!=200){
showIcon('remove');
document.querySelector('#output').innerHTML=`<article class="message message-body is-danger"><div class="message-body"Invalid</div></article>`;
throw Error(response.statusText);
}else{
showIcon('check');
return response.json();
}
}).then(data=>{
let output='';
data.places.forEach(place=>{
output+=`<article class="message is-primary">
<div class="message-header">
<p>Location Info</p>
<button class="delete"></button>
</div>
<div class="message-body">
<ul>
<li><strong>City:</strong>${place['place name']}</li>
<li><strong>State:</strong>${place['state']}</li>
</ul>
</div>
</article>`;
});
document.querySelector('#output').innerHTML=output;
})
.catch(err=>console.log(err));
e.preventDefault();
}
function showIcon(){
document.querySelector('.icon-remove').style.display='none';
document.querySelector('.icon-check').style.display='none';
document.querySelector(`.icon-${icon}`).style.display='inline-flex';
}
function deleteLocation(e){
if(e.target.className=='delete'){
document.querySelector('.message').remove();
document.querySelector('.zip').value='';
document.querySelector('.icon-check').remove();
}
}
</script>
</body>
</html>