473,789 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.g etElementsByTag Name... )

var e;
var f; // Will hold a document fragment.
document.getEle mentsByTagName( "head")[0].appendChild(do cument.createEl ement("title")) ;
document.title = "Page Title";

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

f = document.create DocumentFragmen t();
f.appendChild(d ocument.createE lement("ul"))
e = document.create Element("li");
e.appendChild(d ocument.createT extNode("Item #1"));
f.childNodes[0].appendChild(e) ;
e = document.create Element("li");
e.appendChild(d ocument.createT extNode("Item #2"));
f.childNodes[0].appendChild(e) ;
e = document.create Element("li");
e.appendChild(d ocument.createT extNode("Item #3"));
f.childNodes[0].appendChild(e) ;
document.body.a ppendChild(f);

document.body.a ppendChild(docu ment.createElem ent("div"));

f = document.create DocumentFragmen t();
f.appendChild(d ocument.createE lement("table") );
for (var i=0; i<3; i++) {
e = document.create Element("tr");
e.appendChild(d ocument.createE lement("td"));
e.appendChild(d ocument.createE lement("td"));
e.getElementsBy TagName("td")[0].innerHtml =("C"+i+",0") ;
e.getElementsBy TagName("td")[1].innerHtml = ("C"+i+",1") ;
f.getElementsBy TagName("table" )[0].appendChild(e) ;
}
document.getEle mentsByTagName( "div")[0].appendChild(f) ;

Dec 15 '05 #1
2 2289
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.g etElementsByTag Name... )

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

document.getEle mentsByTagName( "head")[0].appendChild(do cument.createEl ement("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.create Element("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(d ocument.createT extNode('Here ...'));

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

var docBody = document.body || document.docume ntElement;
docBody.appendC hild(e);
may be safer.

f = document.create DocumentFragmen t();
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(d ocument.createE lement("ul"))
f = document.create Element('ul');
e = ...
e = document.create Element("li");
e.appendChild(d ocument.createT extNode("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.create Element("li");
e.appendChild(d ocument.createT extNode("Item #2"));
f.childNodes[0].appendChild(e) ;
If f is used as suggested, replace this line with:

f.appendChild(e );

e = document.create Element("li");
e.appendChild(d ocument.createT extNode("Item #3"));
f.childNodes[0].appendChild(e) ;
If f is used as suggested, replace this line with:

f.appendChild(e );

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

docBody.appendC hild(f);


document.body.a ppendChild(docu ment.createElem ent("div"));
Why not keep using f as above:

f = document.create Element("div");


f = document.create DocumentFragmen t();
Delete.

f.appendChild(d ocument.createE lement("table") );
var t = document.create Element('table' );
var c; // Used below

for (var i=0; i<3; i++) {
e = document.create Element("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(d ocument.createE lement("td"));


c = document.create Element("td");
c.appendChild(d ocument.createT extNode("C" + i + "," + j));
e.appendChild(c );
}
}
f.appendChild(t );
docBody.appendC hild(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
2901
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 no luck though), and searched through the IRT.ORG Faqs (www.irt.org/script/script.htm). The Javascript is designed to open an popup window and then inside that window call another script which will resize that window. There may be another...
2
2421
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 is a dropdownlist control on the page, I add dynamically extra rows to the table. For instance, if the ddlSPType selected item is Aviation Company, an extra row containing a cell with the textbox control txtAircrafts is added to the HTML table...
3
3834
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 dataset to dynamically create a Html Table control. I want to display the table on another frame page (target="main") without the web form controls (i.e. the textbox, search button, and dropdown list). I just want the table displayed only on the...
1
6347
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 dataset to dynamically create a Html Table control. I want to display the table on another frame page (target="main") without the web form controls (i.e. the textbox, search button, and dropdown list). I just want the table displayed only on the...
10
3164
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 ld_tot_pet_clean_fee_calc As Double ld_tot_pet_clean_fee_calc = li_net_total_calc * 0.0075 ld_tot_pet_clean_fee = lo_misc_func.FormatMC(ld_tot_pet_clean_fee_calc, "D") Session("tot_pet_clean_fee") = lo_misc_func.FormatMC(ld_tot_pet_clean_fee, "D")...
2
1269
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 give an example. document.getElementsByTagName("head").appendChild(document.createElement("title")); alert(document.getElementsByTagName("title").tagName);
7
1289
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? document.getElementsByTagName("head").appendChild(document.createElement("title")); alert(document.getElementsByTagName("title").tagName); document.getElementsByTagName("title").appendChild(document.createTextNode("test")); I get an error on the 3rd line: "Unexpected call to methor or property...
1
12396
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 script or c# code required) Regards Eldho
0
3224
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" SourcePos="A2" sourceConc=2"/> </Source> </Sample> <Sample> <Destination Position="D12"/>
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9984
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.