-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
104 lines (77 loc) · 3.17 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
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BuscaCEPs</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
</head>
<style>
.base{
width: 100vw;
height: 100vh;
background: #dcdcde;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center
}
.container{
width: 300px;
height: 800px;
background: #a7aaad;
border-radius: 20px;
}
h1{
margin-top: 10px;
}
label{
font-weight: 500;
}
small{
font-size: 11px;
color: rgba(4, 4, 9, 0.509);
font-style: italic;
}
</style>
<body>
<div class="base">
<div class="container">
<form>
<h1>Consulte o CEP</h1><br>
<div class="form-group"><label for="cep">CEP</label><input type="text" class="form-control" maxlength="9" id="cep"><small>00000-000</small></div><br>
<div class="form-group"><label for="ddd">DDD</label><input type="text" class="form-control" id="ddd"><small>(00)</small></div><br>
<div class="form-group"><label for="logradouro">Logradouro</label><input type="text" class="form-control" id="logradouro"><small>Av./Rua/Travessa</small></div><br>
<div class="form-group"><label for="bairro">Bairro</label><input type="text" class="form-control" id="bairro"><small>Bairro</small></div><br>
<div class="form-group"><label for="localidade">Localidade</label><input type="text" class="form-control" id="localidade"><small>Cidade/Município/Distrito</small></div><br>
<div class="form-group"><label for="uf">UF</label><input type="text" class="form-control" id="uf"><small>Estado</small></div><br>
</form>
</div>
</div>
<script>
const cep = document.querySelector('#cep')
const showData = (result) => {
for(const campo in result){
if(document.querySelector('#'+campo)){
document.querySelector('#'+campo).value = result[campo]
}
}
}
cep.addEventListener('blur', (e)=>{
let search = cep.value.replace('-','')
const options = {
method: 'GET',
mode: 'cors',
cache: 'default'
}
fetch(`https://viacep.com.br/ws/${search}/json/`, options)
.then(response=>{response.json()
.then(data => showData(data))
})
.catch(e => console.log('Deu Erro: '+ e,message))
})
</script>
</body>
</html>