473,890 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to use javascript DOM to insert TABLE, not working

9 New Member
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.create TextNode("cell contents")

var td1 = document.create Element("TD")
td1.appendChild (text1)
var tr1 = document.create Element("TR")
tr1.appendChild (td1)
var table1 = document.create Element("TABLE" )
table1.appendCh ild(tr1)

document.getEle mentById("text" ).appendChild(t able1)
}
</SCRIPT>

<DIV id=text>x</DIV>
<BR>
<INPUT type=button onclick=InitPag e() value=go!>
</BODY>
</HTML>[/HTML]
Apr 30 '08 #1
10 3007
rpnew
188 New Member
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.create TextNode("cell contents")

var td1 = document.create Element("TD")
td1.appendChild (text1)
var tr1 = document.create Element("TR")
tr1.appendChild (td1)
var table1 = document.create Element("TABLE" )
table1.appendCh ild(tr1)

document.getEle mentById("text" ).appendChild(t able1)
}
</SCRIPT>

<DIV id=text>x</DIV>
<BR>
<INPUT type=button onclick=InitPag e() 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 Recognized Expert Moderator Expert
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 New Member
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.ge tElementById("T ableDiv").appen dChild(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.create TextNode("START ")
var td1 = document.create Element("TD")
td1.appendChild (text1)
var tr1 = document.create Element("TR")
tr1.appendChild (td1)
var tbody1 = document.create Element("TBODY" )
tbody1.appendCh ild(tr1)
var table1 = document.create Element("TABLE" )
table1.appendCh ild(tbody1)

document.getEle mentById("Table Div").appendChi ld(table1)
}

</SCRIPT>
</HEAD>

<BODY>
<DIV id=tablediv></DIV>
<BR>
<INPUT type=button onclick=InitPag e() value=go!>
</BODY>
</HTML>[/HTML]
May 1 '08 #4
lxfc97
9 New Member
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 Recognized Expert Moderator MVP
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="InitPa ge()" value="go!">[/html]
Anyway, glad you found the error and thanks for posting.
May 1 '08 #6
lxfc97
9 New Member
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 New Member
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 Recognized Expert Moderator MVP
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 New Member
Hi,

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

Thanks again,
Dave
May 3 '08 #10

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

Similar topics

7
12141
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 servers using sp_addlinkedserver on both database servers. When I call the Commit API of oledb I get the following error: Error state: 1, Severity: 19, Server: TST-PROC22, Line#: 1, msg:
2
4999
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 and the javascript code will rank the contents of the table based on that attribute. This is done. Now I want to record the click information into an access database. basically, wheneve the user click an attribute, I want to use asp code to...
25
4082
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
6
1983
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 = document.getElementById('upload_div'); var num = contador_upload++; var div = document.createElement("div"); var div_name = "filediv"+num; div.setAttribute("id",div_name);
4
1795
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 here%> <script language=javascript> function insert()
8
3224
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 javascript is not working. e.g. html content:
1
3154
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 information is). I've got the product name at the top, left aligned and typically as a two lined header, and I'd like to add a small logo to the right of that, either right beside it, or in the far right corner. could anyone help me with how to set...
10
7314
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. Please help me. Here is the php code: function print_rezBilet($rezBilet,$posRezBilet1){ $step=$_SESSION; $max=$_SESSION; $curr= $_SESSION+$step-1 ; if($max<$curr) $curr=$max;
4
2193
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 not reinsert it with a different revision number. Compounding the issue, we’ve also got an associated table storing properties for our entities which is not revisioned, but we still want changes to the children of our entity (additions, changes...
0
9975
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9812
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
10799
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
10899
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
10446
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
9614
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 projectplanning, coding, testing, and deploymentwithout 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...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4255
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3263
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.