473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Problem dynamically writing html-- table not showing up

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);

Dec 15 '05 #1
2 2264
ezmiller wrote:
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.
f is unnecessary.

document.getElementsByTagName("head")[0].appendChild(document.createElement("title"));
If your page did not already have a title element, it was invalid HTML.

document.title = "Page Title";
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).


e = document.createElement("p");
Features that may not be supported by all browsers/UAs should be tested
before use.

e.setAttribute("id","p1");
e.innerHTML = "Here is a short paragraph.";
Mixing W3 DOM and non-standard innerHTML is not a good idea:

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

document.body.appendChild(e);
Depending on your doctype and browser:

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

f = document.createDocumentFragment();
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:
f.appendChild(document.createElement("ul"))
f = document.createElement('ul');
e = ...
e = document.createElement("li");
e.appendChild(document.createTextNode("Item #1"));
A loop would be much simpler, I guess this is just a sample...

f.childNodes[0].appendChild(e);
If f is used as suggested above:

f.appendChild(e);

e = document.createElement("li");
e.appendChild(document.createTextNode("Item #2"));
f.childNodes[0].appendChild(e);
If f is used as suggested, replace this line with:

f.appendChild(e);

e = document.createElement("li");
e.appendChild(document.createTextNode("Item #3"));
f.childNodes[0].appendChild(e);
If f is used as suggested, replace this line with:

f.appendChild(e);

document.body.appendChild(f);
Using suggested changes, replace this line with:

docBody.appendChild(f);


document.body.appendChild(document.createElement(" div"));
Why not keep using f as above:

f = document.createElement("div");


f = document.createDocumentFragment();
Delete.

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

for (var i=0; i<3; i++) {
e = document.createElement("tr");
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){

e.appendChild(document.createElement("td"));


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
Dec 15 '05 #2
RobG wrote:
ezmiller wrote:
document.title = "Page Title";
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).


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.
[...]
Mixing W3 DOM and non-standard innerHTML is not a good idea:


Ahhh :) (SCNR)
Regards,
PointedEars
Dec 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
2
by: buran | last post by:
Dear ASP.NET Programmers, I have a HTML table (running as server control with the control ID: tblInsertSP). The table has 16 rows with textboxes. Depending on the value of the ddlSPType, which...
3
by: Al Wilkerson | last post by:
Hey, I have a Web Form with a drop down list, textbox, and search button. When click the search button an SQL server database is queried fordata. Once I have the data in a dataset I use the...
1
by: Al Wilkerson | last post by:
Hey, I have a Web Form with a drop down list, textbox, and search button. When click the search button an SQL server database is queried fordata. Once I have the data in a dataset I use the...
10
by: Coleen | last post by:
Hi all :-) I have a weird formatting problem with an HTML table that I am populating using VB.Net and HTML. Here is the snippet of code for the cell I'm trying to format: Dim...
2
by: ezmiller | last post by:
Hi, I am trying to learn how to dynamically write HTML using the W3C DOM. But I am having trouble with the code. It doesn't seem to be doing what I expect it to and I am not sure why. Let me...
7
by: ezmiller | last post by:
I am trying to learn to write code dynamcially using javascript and the W3C DOM. Does anybody know why this code mihgt be giving me trouble? ...
1
by: eldhopvm | last post by:
Hi I need an urgent help.I want the code for exporting an html table to Excel. The table is dynamically generated Html table only.Once the table is generated i want to export it to excel.(java...
0
by: Bogey | last post by:
Hi, I am a complet newby in XML; I just want to create a table for datas coming from XML file <Study StudyNumber="3"> <Sample> <Destination Position="D2"/> <Source SourcePlate="P3"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.