473,405 Members | 2,415 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,405 software developers and data experts.

Trying to use javascript DOM to insert TABLE, not working

9
Hi,

I'm trying to use the DOM to insert a TABLE into a DIV tag.

Can anyone tell me what I'm doing wrong here?

The code looks straightforward, but it is not working.

Thanks,
Dave
--------------------------------------------------------------
[HTML]<HTML>
<BODY>
<SCRIPT>
function InitPage() {

var text1 = document.createTextNode("cell contents")

var td1 = document.createElement("TD")
td1.appendChild(text1)
var tr1 = document.createElement("TR")
tr1.appendChild(td1)
var table1 = document.createElement("TABLE")
table1.appendChild(tr1)

document.getElementById("text").appendChild(table1 )
}
</SCRIPT>

<DIV id=text>x</DIV>
<BR>
<INPUT type=button onclick=InitPage() value=go!>
</BODY>
</HTML>[/HTML]
Apr 30 '08 #1
10 2977
rpnew
188 100+
Hi,

I'm trying to use the DOM to insert a TABLE into a DIV tag.

Can anyone tell me what I'm doing wrong here?

The code looks straightforward, but it is not working.

Thanks,
Dave
--------------------------------------------------------------
[HTML]<HTML>
<BODY>
<SCRIPT>
function InitPage() {

var text1 = document.createTextNode("cell contents")

var td1 = document.createElement("TD")
td1.appendChild(text1)
var tr1 = document.createElement("TR")
tr1.appendChild(td1)
var table1 = document.createElement("TABLE")
table1.appendChild(tr1)

document.getElementById("text").appendChild(table1 )
}
</SCRIPT>

<DIV id=text>x</DIV>
<BR>
<INPUT type=button onclick=InitPage() value=go!>
</BODY>
</HTML>[/HTML]
Hi,
Your code is working with me. what error do you get?
Are you getting text 'cell contents' on web page?

Regards,
RP
Apr 30 '08 #2
gits
5,390 Expert Mod 4TB
in IE you have to append to the <tbody> ... have a look here for some browser specific inconsitencies ...

kind regards
Apr 30 '08 #3
lxfc97
9
Hi,

Thanks very much for taking the time to respond to my query. I got my code to work in IE 6 by adding the TBODY to the table creation code.
However, even with the TBODY in the table, the code doesn't work in Netscape or Firefox. In fact, in the code below, the statement ****document.getElementById("TableDiv").appendChil d(table1)**** halts execution of the NS 6 browser (there is no error message).

Would anybody know why?

I apologize if this is an obvious queston.

Thanks very much,
Dave

Here's the updated code:
----------
[HTML]<HTML>
<HEAD>
<SCRIPT>

function InitPage() {

var text1 = document.createTextNode("START")
var td1 = document.createElement("TD")
td1.appendChild(text1)
var tr1 = document.createElement("TR")
tr1.appendChild(td1)
var tbody1 = document.createElement("TBODY")
tbody1.appendChild(tr1)
var table1 = document.createElement("TABLE")
table1.appendChild(tbody1)

document.getElementById("TableDiv").appendChild(ta ble1)
}

</SCRIPT>
</HEAD>

<BODY>
<DIV id=tablediv></DIV>
<BR>
<INPUT type=button onclick=InitPage() value=go!>
</BODY>
</HTML>[/HTML]
May 1 '08 #4
lxfc97
9
Hi,

Thanks very much for the help, but I found the NS error.
I had caps in TableDiv in the addChild statement, but all
lower caps for the ID attribute in the HTML.

Sorry for the confusion.

Again, thank you very much for the help.

Dave
May 1 '08 #5
acoder
16,027 Expert Mod 8TB
If you had checked the Firefox error console, you would've found this message:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("TableDiv") has no properties
Don't test in IE first otherwise you won't pick up these errors. That should never have worked in IE because JavaScript is case-sensitive and if the case differs the ID is different.

One final note: you should quote your attributes, e.g.
[html]<DIV id="text">x</DIV>
<BR>
<INPUT type="button" onclick="InitPage()" value="go!">[/html]
Anyway, glad you found the error and thanks for posting.
May 1 '08 #6
lxfc97
9
Hi,

Thanks for the info.
I'll quote my attributes from now on (I wasn't because I didn't think I needed to,
but it's always better to follow standards).

I'll also check the Netscape/Firefox error console from now on.

Thanks again very much,
Dave
May 2 '08 #7
lxfc97
9
IHi,

'm not sure if anybody is still reading this thread but I just figured out
that if you want to be XHTML compliant then you need to quote your
attributes.

Thanks again,
Dave
May 2 '08 #8
acoder
16,027 Expert Mod 8TB
I just figured out that if you want to be XHTML compliant then you need to quote your attributes.
and your tag names and attributes should be lower-case, elements must be closed, etc. but you'd figure that out when you validate.
May 2 '08 #9
lxfc97
9
Hi,

Thanks for the info.
It'd take me a while to figure that out while validating. :)

Thanks again,
Dave
May 3 '08 #10
acoder
16,027 Expert Mod 8TB
No problem, you're welcome :)
May 3 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: iqbal | last post by:
Hi all, We have an application through which we are bulk inserting rows into a view. The definition of the view is such that it selects columns from a table on a remote server. I have added the...
2
by: Tinderbox | last post by:
I am working on a piece of code for an academic experiment and it puzzled me for days, any help? I use javascript to sort a table in a html page. bascially, a user can click on any attributes...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
6
by: ApOG | last post by:
Hello everyone, I have this javascript code working perfectly with IE, but with firefox nothing happens when running the function... function add_div_field () { var ni =...
4
by: deedi | last post by:
Hi, I'm writing a code which calls the javascript function through button click in ASP. But i fear , the function is executed before onclick. Here is my code. <html> <body> <%asp code...
8
by: Pratik Patel | last post by:
Hello, I used innerHTML to assign HTML content. but in my HTML page content have also some javascript function and it will run when page load. bu when HTML code assgin thru innerHTML then this...
1
by: rsteph | last post by:
I've got some product information pages, with images and text, all setup within a table. I'm trying to add a small image in the upper right hand corner of the content div (where all the important...
10
by: evicailieva | last post by:
A have a php scrip where I call a JavaScript function. I don't know why, but it doesn't work. At the beginning, when I was writing the script it was working but now it's not. I don't know wхat to do....
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.