-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (39 loc) · 1.35 KB
/
script.js
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
document.querySelector('.busca').addEventListener('submit', async (event) => {
event.preventDefault();
let input = document.querySelector('#searchInput').value;
if(input ){
clearInfo();
showWarning('Carregando dados...');
let url = `https://viacep.com.br/ws/${encodeURIComponent(input)}/json/`;
let results = await fetch(url);
let json = await results.json();
let cod = results.status;
showInfo({
bairro:json.bairro,
cep: json.cep,
logradouro:json.logradouro,
localidade: json.localidade,
uf: json.uf
})
}
else{
clearInfo();
showWarning("Insira um CEP!")
}});
//mensagem de erro
function showWarning(msg){
document.querySelector('.aviso').innerHTML = msg;
};
//armazenar dados do cep
function showInfo(json){
showWarning('');
document.querySelector('.titulo').innerHTML = `${json.localidade}, ${json.uf}`;
document.querySelector('.endInfo').innerHTML = `${json.logradouro}`;
document.querySelector('.bairroInfo').innerHTML = `${json.bairro}`;
document.querySelector('.titulo2').innerHTML = `${json.cep}`;
document.querySelector('.resultado').style.display = "block"
}
function clearInfo() {
showWarning('');
document.querySelector('.resultado').style.display = 'none';
}