Connecting Tech Pros Worldwide Help | Site Map

Problem dynamically writing html-- table not showing up

ezmiller
Guest
 
Posts: n/a
#1: Dec 15 '05
Hi,
I have some code (which I will paste in below) that writes out some
HTML dynamically using the W3C DOM...the last part of the code write
out a simple table. MY problem is that the table is not showing up,
even though it seems to be part of the document (i.e. I can reach the
nodes through through document.body.getElementsByTagName... )

var e;
var f; // Will hold a document fragment.


document.getElementsByTagName("head")[0].appendChild(document.createElement("title"));
document.title = "Page Title";

e = document.createElement("p");
e.setAttribute("id","p1");
e.innerHTML = "Here is a short paragraph.";
document.body.appendChild(e);

f = document.createDocumentFragment();
f.appendChild(document.createElement("ul"))
e = document.createElement("li");
e.appendChild(document.createTextNode("Item #1"));
f.childNodes[0].appendChild(e);
e = document.createElement("li");
e.appendChild(document.createTextNode("Item #2"));
f.childNodes[0].appendChild(e);
e = document.createElement("li");
e.appendChild(document.createTextNode("Item #3"));
f.childNodes[0].appendChild(e);
document.body.appendChild(f);

document.body.appendChild(document.createElement(" div"));

f = document.createDocumentFragment();
f.appendChild(document.createElement("table"));
for (var i=0; i<3; i++) {
e = document.createElement("tr");
e.appendChild(document.createElement("td"));
e.appendChild(document.createElement("td"));
e.getElementsByTagName("td")[0].innerHtml =("C"+i+",0");
e.getElementsByTagName("td")[1].innerHtml = ("C"+i+",1");
f.getElementsByTagName("table")[0].appendChild(e);
}
document.getElementsByTagName("div")[0].appendChild(f);

RobG
Guest
 
Posts: n/a
#2: Dec 15 '05

re: Problem dynamically writing html-- table not showing up


ezmiller wrote:[color=blue]
> Hi,
> I have some code (which I will paste in below) that writes out some
> HTML dynamically using the W3C DOM...the last part of the code write
> out a simple table. MY problem is that the table is not showing up,
> even though it seems to be part of the document (i.e. I can reach the
> nodes through through document.body.getElementsByTagName... )
>
> var e;
> var f; // Will hold a document fragment.[/color]

f is unnecessary.

[color=blue]
> document.getElementsByTagName("head")[0].appendChild(document.createElement("title"));[/color]

If your page did not already have a title element, it was invalid HTML.

[color=blue]
> document.title = "Page Title";[/color]

I'm not sure that modifying the document title property is the same as
changing the content of the title element... too late for testing, I'll
leave that to you (or someone else).

[color=blue]
>
> e = document.createElement("p");[/color]

Features that may not be supported by all browsers/UAs should be tested
before use.

[color=blue]
> e.setAttribute("id","p1");
> e.innerHTML = "Here is a short paragraph.";[/color]

Mixing W3 DOM and non-standard innerHTML is not a good idea:

e.appendChild(document.createTextNode('Here ...'));

[color=blue]
> document.body.appendChild(e);[/color]

Depending on your doctype and browser:

var docBody = document.body || document.documentElement;
docBody.appendChild(e);


may be safer.

[color=blue]
> f = document.createDocumentFragment();[/color]

You don't need the document fragment. You append everything to the UL,
then append that to the document so just create f as a reference to the
UL and go from there:
[color=blue]
> f.appendChild(document.createElement("ul"))[/color]

f = document.createElement('ul');
e = ...
[color=blue]
> e = document.createElement("li");
> e.appendChild(document.createTextNode("Item #1"));[/color]

A loop would be much simpler, I guess this is just a sample...

[color=blue]
> f.childNodes[0].appendChild(e);[/color]

If f is used as suggested above:

f.appendChild(e);

[color=blue]
> e = document.createElement("li");
> e.appendChild(document.createTextNode("Item #2"));
> f.childNodes[0].appendChild(e);[/color]

If f is used as suggested, replace this line with:

f.appendChild(e);

[color=blue]
> e = document.createElement("li");
> e.appendChild(document.createTextNode("Item #3"));
> f.childNodes[0].appendChild(e);[/color]

If f is used as suggested, replace this line with:

f.appendChild(e);

[color=blue]
> document.body.appendChild(f);[/color]

Using suggested changes, replace this line with:

docBody.appendChild(f);

[color=blue]
>
> document.body.appendChild(document.createElement(" div"));[/color]

Why not keep using f as above:

f = document.createElement("div");

[color=blue]
>
> f = document.createDocumentFragment();[/color]

Delete.

[color=blue]
> f.appendChild(document.createElement("table"));[/color]

var t = document.createElement('table');
var c; // Used below

[color=blue]
> for (var i=0; i<3; i++) {
> e = document.createElement("tr");[/color]

When adding rows in IE, you must add them to a tbody element not the
table element. Simpler to use insertRow, so replace the above line with:

e = t.insertRow(i);
for (var j=0; j<2; ++j){

[color=blue]
> e.appendChild(document.createElement("td"));[/color]

c = document.createElement("td");
c.appendChild(document.createTextNode("C" + i + "," + j));
e.appendChild(c);
}
}
f.appendChild(t);
docBody.appendChild(f);


Untested, but should be OK... snooze time.



--
Rob
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#3: Dec 15 '05

re: Problem dynamically writing html-- table not showing up


RobG wrote:
[color=blue]
> ezmiller wrote:[color=green]
>> document.title = "Page Title";[/color]
>
> I'm not sure that modifying the document title property is the same as
> changing the content of the title element... too late for testing, I'll
> leave that to you (or someone else).[/color]

Unfortunately, it is not the same in practice. But modifying the value of
the `nodeValue' property of the child text node does not have any effect on
display, only on the DOM tree (that did not change in Firefox 1.5). See a
previous discussion about it.
[color=blue]
> [...]
> Mixing W3 DOM and non-standard innerHTML is not a good idea:[/color]

Ahhh :) (SCNR)


Regards,
PointedEars
Closed Thread