-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (32 loc) · 984 Bytes
/
script.js
File metadata and controls
45 lines (32 loc) · 984 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
async function buscarCEP(){
const cep = document.getElementById("cep").value;
if(cep.length !== 8){
alert("Digite um CEP válido!");
return;
}
const url = `https://viacep.com.br/ws/${cep}/json/`;
const resposta = await fetch(url);
const dados = await resposta.json();
if(dados.erro){
alert("CEP não encontrado!");
return;
}
document.getElementById("rua").textContent = dados.logradouro;
document.getElementById("bairro").textContent = dados.bairro;
document.getElementById("cidade").textContent = dados.localidade;
document.getElementById("estado").textContent = dados.uf;
const textoQR = `
${dados.logradouro},
${dados.bairro},
${dados.localidade} - ${dados.uf},
CEP: ${dados.cep}
`;
document.getElementById("qrcode").innerHTML = "";
QRCode.toCanvas(textoQR, { width: 250 }, function(err, canvas){
if(err){
console.error(err);
return;
}
document.getElementById("qrcode").appendChild(canvas);
});
}