- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
Latest commit
31 lines (24 loc) · 920 Bytes
/
Copy pathscript.js
File metadata and controls
31 lines (24 loc) · 920 Bytes
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
varcep=document.getElementById('cep');
varcidade=document.getElementById('cidade');
varlogradouro=document.getElementById('logradouro');
varestado=document.getElementById('estado');
varerro=document.getElementById('erro');
cep.addEventListener("focusout",()=>buscaEndereco(cep.value));
asyncfunctionbuscaEndereco(cep){
erro.innerHTML="";
try{
varconsultaCEP=awaitfetch(`https://viacep.com.br/ws/${cep}/json/`);
varCEPConvertido=awaitconsultaCEP.json();
if(CEPConvertido.erro){
throwError('CEP inexistente!');
}
cidade.value=CEPConvertido.localidade;
logradouro.value=CEPConvertido.logradouro;
estado.value=CEPConvertido.uf;
console.log(CEPConvertido);
returnCEPConvertido;
}
catch(erro){
erro.innerHTML='<p> CEP inválido. Tente Novamente! </p>';
}
}