-
Notifications
You must be signed in to change notification settings - Fork 0
/
prova2.html
79 lines (55 loc) · 1.43 KB
/
prova2.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
<!DOCTYPE html>
<html>
<title> Cercador codis postals </title>
<body>
<h1> Cercador codis postals </h1>
Codi postal:<input type="text" id="text_cp">
<br />
Num. resultats: <input type="text" id="text_resultats">
<br/>
<input type="button" value="Click" id="the_button">
<br />
<br />
<table id="taula-cps">
<thead>
<tr>
<th>Codi Postal</th>
<th>Ciutat</th>
</tr>
</thead>
<tbody id="dades-taula">
<tr>
<td>cp prova</td>
<td>ciutat prova</td>
</tr>
</tbody>
</table>
<script>
document
.getElementById("the_button")
.addEventListener("click",() =>{
var cp=document.getElementById("text_cp").value;
var nresultats= document.getElementById("text_resultats").value;
var url='http://api.geonames.org/postalCodeSearchJSON?postalcode_startsWith='+cp+'&country=ES&maxRows='+nresultats+'&username=nelaq_';
console.log(url);
fetch(url)
.then(resposta => resposta.json ())
.then(json => {
html=""
json.postalCodes.forEach(function(element){
console.log(element.placeName);
html +="<tr>";
html += "<td>"+element.postalCode+"</td>";
html += "<td>"+element.placeName+"</td>";
html += "</tr>";
});
console.log(html);
document
.getElementById("dades-taula")
.innerHTML = html;
})
.catch(error => console.log (error));
});
</script>
</body>
</html>