-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring2object.html
47 lines (36 loc) · 1.01 KB
/
string2object.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
<html>
<meta charset="utf-8" />
<body>
<style>
table {
border-collapse: collapse;
}
td {
padding: 10;
margin: 0;
border: 1px darkred solid;
}
</style>
<div>
<textarea id="input" style="width: 95%;height: 180px;">
</textarea>
<button onclick="showAsTable();">Visa som tabell</button>
</div>
<hr />
<div id="output"></div>
<script>
// deklarera ett object
function showAsTable() {
var npt = document.getElementById("input").value;
var movieData = JSON.parse(npt); // tolka string till ett JS-objekt
// det hela som tabell
var tabellkod = "<table>";
for (let [key, value] of Object.entries(movieData)) {
tabellkod += "<tr><td>" + key + "</td><td>" + value + "</td></tr>";
}
tabellkod += "</table>";
document.getElementById("output").innerHTML = tabellkod;
}
</script>
</body>
</html>