473,666 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help w/ appending nodes

I am trying to create a script to enter the values of an array into a
dynamically generated table 3 columns wide. I have a counter for the
row # that I am using to name/id the row TR node so I an attach 3
cells (3 columns) before attaching the full row to the table. I'm not
sure where the problem is, but in its current form my script doesn't
even generate the first node before stopping with an error. Please let
me know what I'm doing wrong.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
function startup()
{
colcount = 0;
rowcount = 0;
var List = new
Array('one','tw o','three','fou r','five','six' ,'seven');
var Count = 7;
tabBody=documen t.getElementsBy TagName("TBODY" ).item(0);
for (var i = 0; i<Count; i++)
{
if (colcount == 0)
{
var row=document.cr eateElement("TR ");
rowcount++;
row.id = rowcount;
}
cell = document.create Element("TD");
textnode=docume nt.createTextNo de(List[i]);
cell.appendChil d(textnode);
document.getEle mentById(rowcou nt).appendChild (cell);
colcount++;
if (colcount == 3)
{
tabBody.appendC hild(row);
colcount = 0;
}
}
}
</script>
</head>
<body onload="startup ()">
<table border='1' id='mytable'>
<tbody>
</tbody>
</table>
</body>
</html>
Jun 27 '08 #1
1 1196
On Apr 30, 8:02 am, irixd...@gmail. com wrote:
I am trying to create a script to enter the values of an array into a
dynamically generated table 3 columns wide. I have a counter for the
row # that I am using to name/id the row TR node so I an attach 3
cells (3 columns) before attaching the full row to the table. I'm not
sure where the problem is, but in its current form my script doesn't
even generate the first node before stopping with an error. Please let
me know what I'm doing wrong.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
function startup()
{
colcount = 0;
rowcount = 0;
If you intend these to be global variables, better to declare them
with var outside the function to show that is the clear intent.

var List = new
Array('one','tw o','three','fou r','five','six' ,'seven');
Variables starting with a capital letter are normally reserved for
constructors (just a convention, but good to follow). Also,
initialisers are considered better practice:

var list = ['one','two','th ree','four','fi ve','six','seve n'];

var Count = 7;
tabBody=documen t.getElementsBy TagName("TBODY" ).item(0);
Why is tabBody global? You could also use:

var tabBody=documen t.getElementsBy TagName("TBODY" )[0];

for (var i = 0; i<Count; i++)
{
if (colcount == 0)
{
var row=document.cr eateElement("TR ");
You can also use the more convenient insertRow:

row = tabBody.insertR ow(-1);

which creates the row and inserts it in one statement.

rowcount++;
row.id = rowcount;
Though usually tolerated, an ID attribute starting with a number is
invalid:

<URL: http://www.w3.org/TR/html4/types.html#type-name >

}
cell = document.create Element("TD");
Consider using:

row.insertCell(-1);

textnode=docume nt.createTextNo de(List[i]);
cell.appendChil d(textnode);
document.getEle mentById(rowcou nt).appendChild (cell);
Firebug gives the following error:

document.getEle mentById(rowcou nt) has no properties

because the element with the id rowcount hasn't been added to the
document yet. But the expression isn't needed anyway since you
already have a reference to that element as "row".

row.appendChild (cell);

colcount++;
if (colcount == 3)
{
tabBody.appendC hild(row);
colcount = 0;
}
}}
Consider something like:

function startup() {
var cell, row;
var colcount = 0;
var list = ['one','two','th ree','four','fi ve','six','seve n'];
var tBody = document.getEle mentsByTagName( "TBODY")[0];

for (var i=0, len=list.length ; i<len; i++) {
if (!(colcount%3)) {
row = tBody.insertRow (-1);
}
cell = row.insertCell(-1);
cell.appendChil d(document.crea teTextNode(list[i]));
++colcount;
}
}
A bit of feature detection and defensive programming would be good too
(e.g test that document.getEle mentsByTagName is supported and that
document.getEle mentsByTagName( "TBODY") returns something before
attempting to access its properties).
--
Rob
Jun 27 '08 #2

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

Similar topics

0
2674
by: Gennadiy Tit | last post by:
I have to load 1700 records from DB into tree view (NOT LIST VIEW). I have tried to load it with locking the windows and control and other stuff, but it not givingme the result that I need. It takes around 18 minutes to load them all. See I am loading those record not in one straight line (in tree view control), but like this: ******************************** 1ST MAIN CATEGORY -- 1ST SUB-- SUB--
0
4347
by: Mark Rezansoff | last post by:
I have two examples of populating a treeview with AD information below. example A does exactly what I want, starts at the AD tree, but is really slow to generate. examble B gives more information, but is really fast. example B displays all the providers (ie. WinNT, NWcompat, NDS, IIS, and LDAP) I just want it to start at the top level of my AD tree. (LDAP only) This way the end user (help desk) can drill down the tree
2
2842
by: csx | last post by:
Hi all, I'm trying to count the number of leafnodes for a particular node. What im trying to do is make a function, that taking the tree structure: key row desc parent 1 1 A 0 2 2 B 1 3 2 C 1 4 3 D 3
3
2172
by: serge calderara | last post by:
Dear all, I have a csv file data which has been read and populate on a dataset object. then I need to bind part of the content of the dataset to a treeview control. I have read that XML format is particular usefull to be bind to a treeview as it handle nodes. So I have try to save my dataset content into a xml file. The content of that file is as follow : - <NewDataSet> - <REC_INFO>
0
1405
by: Patrick.O.Ige | last post by:
I have this code below. All its suppose to do is to EXPAND / COLLAPSE ALL of my treeview. But when i use ASP.NET WEbmatrix it all works fine .. But with VS.NET is says TreeNodeCollection not defined and TreeNode not defined!!!!!!!!!!!!!!arg!!!! How am i suppose to define it in VS.NET.. Any help appreciated!
4
1251
by: Patrick.O.Ige | last post by:
Hi All, Can anybody help in converting the code below to VB.NEt thanks. I need the loop converted.. thanks Private Sub expandCollapseAllTreeviewNodes(ByVal nodes As TreeNodeCollection, ByVal expand As Boolean) Dim node As TreeNode 'Loop through each node of the collection and expand it. For Each node In nodes
2
3184
by: Shaurya Vardhan | last post by:
Hi, On Appending a child in XML Node, I am having error, "Run-time exception thrown : System.ArgumentException - The node to be inserted is from a different document context." How to resolve this ? I am attaching image of error message. Thanx & Regards, Shaurya Vardhan
3
1620
by: hharry | last post by:
Hi All, If I have the following xml: <Response> <AddressDetails> <Address> </Address> </AddressDetails> </Response>
1
1699
by: Sisnaz | last post by:
I'm sending a message from VB.net (2003) to a C++ app via TCP sockets of values 1 to 328. The message is a WORD value where I have to manage both bytes for the WORD. I'm sending and receiving data from the C++ app with no problem between the values of 1 to 127 and 256 to 328, but the application receives garabage between 128 and 255. My current test syntax is as follows: Dim nodeid As Integer = CType(txtMessage.Text, Integer) Dim...
13
2609
by: sherifffruitfly | last post by:
Hi all, I'm trying to distill all of the info from google searches into what I need, with partial success. In truth, the whole xmlNode, Document, Element, etc group of classes & methods is going over my head - lol! The structure of the xml file I'm trying to append to is as follows: <?xml version="1.0" encoding="UTF-8"?> <!-- stuff -->
0
8449
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
8360
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
8784
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8642
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
5666
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();...
1
2774
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
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.