A
Anthony Levensalor
Evening, gents!
I've got two different methods for adding tables to the dom, and curious
as to which approach might be better, and why. If anyone has the time
and/or the inclination to reply, thanks in advance.
Method 1:
var table = document.createElement("table")
var tbody = document.createElement("tbody")
var hRow = document.createElement("tr")
var cells = [];
var cells[0] =
[
document.createElement("td"),
document.createElement("td"),
document.createElement("td")
]
for (var i = 0; i < cells[0].length; ++i) {
hRow.appendChild(cells[0])
}
tbody.appendChild(hRow)
table.appendChild(tbody)
someParentElement.appendChild(table)
Method 2:
var table = document.createElement("table")
var hRow = table.insertRow(-1)
var cells = []
var cells[0] =
[
hRow.insertCell(0),
hRow.insertCell(1),
hRow.insertCell(2)
]
someParentElement.appendChild(table)
I checked out the ECMA Script Language Binding index
(http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html),
and I can't see anything necessarily good or bad about one way or the
other, save for the fact that Method 1 above has a lot more typing.
I've used both, started out with #1 some time back and went to #2 when I
found out it was available, is there some reason you guys think one is
any better than the other?
All the best,
~A!
I've got two different methods for adding tables to the dom, and curious
as to which approach might be better, and why. If anyone has the time
and/or the inclination to reply, thanks in advance.
Method 1:
var table = document.createElement("table")
var tbody = document.createElement("tbody")
var hRow = document.createElement("tr")
var cells = [];
var cells[0] =
[
document.createElement("td"),
document.createElement("td"),
document.createElement("td")
]
for (var i = 0; i < cells[0].length; ++i) {
hRow.appendChild(cells[0])
}
tbody.appendChild(hRow)
table.appendChild(tbody)
someParentElement.appendChild(table)
Method 2:
var table = document.createElement("table")
var hRow = table.insertRow(-1)
var cells = []
var cells[0] =
[
hRow.insertCell(0),
hRow.insertCell(1),
hRow.insertCell(2)
]
someParentElement.appendChild(table)
I checked out the ECMA Script Language Binding index
(http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html),
and I can't see anything necessarily good or bad about one way or the
other, save for the fact that Method 1 above has a lot more typing.
I've used both, started out with #1 some time back and went to #2 when I
found out it was available, is there some reason you guys think one is
any better than the other?
All the best,
~A!