-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddd.html
51 lines (46 loc) · 1.36 KB
/
ddd.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
<!DOCTYPE html>
<html lang="en">
<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>API fetch</title>
<style>
input {
margin-bottom: 3px;
}
</style>
</head>
<body>
<h1>Insere seu DDD Aqui</h1>
<form>
<input type="text" id="ddd" placeholder="DDD" size="11">
<br>
<input type="text" id="estado" placeholder="Estado" size="3">
<br>
<ul id="lista"></ul>
</form>
<script>
const inputcep = document.querySelector("#ddd")
inputcep.addEventListener('change', buscadadosAPI)
function buscadadosAPI (evento){
const ddd = evento.target.value
fetch(`https://brasilapi.com.br/api/ddd/v1/${ddd}`)
.then(resposta => resposta.json())
.then(dados => {
atribuivalor('estado', dados.state)
const lista = document.getElementById('lista')
lista.innerHTML = ''
dados.cities.forEach(element => {
li = document.createElement('li')
li.innerHTML = element
lista.appendChild(li)
});
})
function atribuivalor(id, valor) {
document.querySelector(`#${id}`).value = valor
}
}
</script>
</body>
</html>