D
Daz
Hi everyone. Sorry for the confusing subject, I couldn't think how best
to word it.
What I would like to know, is if there is an equivilant to this code,
in using JSON.
<script type="text/javascript">
function makeTable(number_of_rows) {
var table = document.createElement('table');
table.border = 1;
number_of_rows = (number_of_rows < 0) ? 0 :
number_of_rows;
number_of_rows = (number_of_rows > 100) ? 100 :
number_of_rows;
for (var i = 0; i < number_of_rows; i++)
{
table.appendChild(new tableRow);
}
return table;
function tableRow()
{
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
tr.appendChild(td1);
tr.appendChild(td2);
td1.textContent = 'Some Data';
td2.textContent = 'Some More Data';
return tr;
}
}
var newTable = new makeTable(5);
document.body.appendChild(newTable);
</script>
Note that the makeTable constructer is calling another constructer
(tableRow) several times, and making a totally new row each time. Would
it also be possible for me to call upon the constructer for tableRow
from outside of the makeTable object? For example:
var newRow = new makeTable.tableRow();
How would I be able to do this? I have read endless tutorials, but I
can't seem to find the answer. Perhaps the answer is obvious, and I
just haven't realised?
All the best.
Daz.
to word it.
What I would like to know, is if there is an equivilant to this code,
in using JSON.
<script type="text/javascript">
function makeTable(number_of_rows) {
var table = document.createElement('table');
table.border = 1;
number_of_rows = (number_of_rows < 0) ? 0 :
number_of_rows;
number_of_rows = (number_of_rows > 100) ? 100 :
number_of_rows;
for (var i = 0; i < number_of_rows; i++)
{
table.appendChild(new tableRow);
}
return table;
function tableRow()
{
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
tr.appendChild(td1);
tr.appendChild(td2);
td1.textContent = 'Some Data';
td2.textContent = 'Some More Data';
return tr;
}
}
var newTable = new makeTable(5);
document.body.appendChild(newTable);
</script>
Note that the makeTable constructer is calling another constructer
(tableRow) several times, and making a totally new row each time. Would
it also be possible for me to call upon the constructer for tableRow
from outside of the makeTable object? For example:
var newRow = new makeTable.tableRow();
How would I be able to do this? I have read endless tutorials, but I
can't seem to find the answer. Perhaps the answer is obvious, and I
just haven't realised?
All the best.
Daz.