...

View Full Version : DOM - Create multiple elements?



Tails
09-15-2005, 07:27 AM
To use the DOM instead of writing innerHTML, is it possible to write an entire table using javascript in a much easier way than hundreds of lines of creating and appending each element and their content?



document.write('<table border="1"><tr><th>Heading</th></tr></table>')


vs



A=document.createElement('table')
A.setAttribute('border','1')

B=document.createElement('tr')
A.appendChild(B)

C=document.createElement('th')
B.appendChild(C)

D=document.createTextNode('heading')
C.appendChild(D)

document.getElementsByTagName('body').item(0).appendChild(A)


I'm not sure if this would be considered a fragment or if such a solution exists. But the tables I'm creating are fairly constant except for the content, and will be used several times on one page. My goal is to dynamically modify the looks of the user display area in a forum, viewing a thread (area showing username, posts, etc) but without using innerHTML (which I only recently realized was proprietary :rolleyes:). Is there a shortcut to accomplishing this using less script?

vwphillips
09-15-2005, 08:20 AM
one way is to have a 'hidden' table

clone that and insert when and where required

jbezweb
09-15-2005, 01:32 PM
Yeah for sure the appending approach looks ugly.

Have a pre-made table hidden in a textarea or hidden field.

Then just load it when you need it.


<textarea id="hiddenTable" style="display:none">

...table code...

</textarea>


<script>
var tableCode = document.getElementById("hiddenTable").value;
</script>

martin_narg
09-15-2005, 02:08 PM
The ever-informative quirksmode has some info on this:
http://www.quirksmode.org/dom/innerhtml.html

m_n



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum