
Originally Posted by
motion-ex
in other words let say you have an object named person with properties: age, sex, location, job, phone...
how do you get the properties of the object and then display them in an html table for example?
You don't use document.write. You'll have to use either the DOM API, or inject HTML with the innerHTML property of some element. For example, using the DOM API:
HTML Code:
<html>
<head>
<script>
var person = {age: 30, sex: "male", location: "Wherever I please"};
window.onload = function() {
var output = document.getElementById("output");
var table = output.appendChild(
document.createElement("table"));
var tbody = table.appendChild(
document.createElement("tbody"));
var tr = tbody.appendChild(
document.createElement("tr"));
for (var key in person) {
var td = tr.appendChild(
document.createElement("td"));
td.appendChild(document.createTextNode(person[key]));
}
}
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
There are much more elegant ways of doing the same thing, but this is the dogmatic approach.
Bookmarks