473,387 Members | 1,548 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,387 software developers and data experts.

can't dynamically generate table in Internet explorer

Hello all,

I'm a novice Javascript programmer, and I'm having browser
compatability issues regarding dynamic generation of tables. The
following content generates "hello" in Firefox 1.5, but just a blank
page in IE 6. Can anybody tell me what I should change to make IE
happy? Even better, is there any kind of tutorial out there on
dynamically generating tables?

Many thanks,
Aric
<html>
<head>
<titleTest </title>
<script language="javascript">
<!--

window.onload = init;

function init() {

var el = document.createElement('table');
var row = document.createElement('tr');
var col = document.createElement('td');
col.appendChild(document.createTextNode('hello'));
row.appendChild(col);
el.appendChild(row);
document.getElementById('mydiv').appendChild(el);
}

// -->
</script>
</head>
<body>
<div ID="mydiv">
</div>
</body>
</html>

Oct 13 '06 #1
2 2562
ar********@gmail.com wrote:
Hello all,

I'm a novice Javascript programmer, and I'm having browser
compatability issues regarding dynamic generation of tables. The
following content generates "hello" in Firefox 1.5, but just a blank
page in IE 6. Can anybody tell me what I should change to make IE
happy?
Every HTML table must have a tbody element, though the tags in the HTML
source are optional. When creating a table using either HTML or
script, the tbody element is automatically added by the browser.

When using DOM Core methods createElement and appendChild to add rows
to a table, most browsers will allow you to append the row to the table
object, even though strictly speaking it should be appended as a child
of the tbody. IE doesn't do that, you have to add it to the tbody.

The simple solution is to use the DOM HTML methods, insertRow and
insertCell[1]. Here IE is also a little different in that if you don't
supply a parameter, it will add the row or cell as the last in the
collection, most other browsers will barf. If you want the new row as
the last row, just use insertRow(-1).

Even better, is there any kind of tutorial out there on
dynamically generating tables?
There are some, but I wouldn't recommend them. The principles are
fairly simple: create a tables using createElement, then add rows using
the table's insertRow() method and cells using the row's insertCell()
method. Remove rows using table.deleteRow().

If you want to create table sections, you can do that too: they have
the same methods as tables do for inserting and deleting rows.

The following link is to the Gecko DOM Table Interface page. It's not
complete, though the examples are better than some - it also has links
to the appropriate W3C specifications.

<URL:
http://developer.mozilla.org/en/docs...ment_Interface
>
>
Many thanks,
Aric
<html>
<head>
<titleTest </title>
<script language="javascript">
The language attribute is deprecated, type is required:

<script type="text/javascript">
<!--
Do not use HTML comment delimiters inside script elements, they are
completely unnecessary and often cause problems.
>
window.onload = init;

function init() {

var el = document.createElement('table');
var row = document.createElement('tr');
var row = el.insertRow(-1);
var col = document.createElement('td');
var col = row.insertCell(-1);
col.appendChild(document.createTextNode('hello'));
If using insertRow/Cell, they are already added, there is no need to
explicitly append them so the next two lines are not required.
row.appendChild(col);
el.appendChild(row);
But you still need to add the table to the document of course. :-)
document.getElementById('mydiv').appendChild(el);
}
[...]

1.
insertCell:
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >

insertRow:
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >

--
Rob

Oct 13 '06 #2
Rob,

Thanks for the helpful information and the clear way you explained
things. That was just what I needed.

Much obliged,
Aric

Oct 13 '06 #3

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

Similar topics

13
by: Sheperd | last post by:
Hey everyone, I have this browser detect script and the validator is having problems with it, im trying to validate a page for XHTML 1.0 Transitional If you have any ideas or suggestions they...
9
by: pablo | last post by:
Dear NGers, I would like to change the alt-text with the changing of the image during a mouseover action. Can document.images.altView be changed dynamically? TIA, pablo
10
by: Melissa Mussitsch | last post by:
I've done this before while creating a brand new table. But the code below is not working and I keep getting the error: "Internet Explorer cannot open the Internet site ..... Operation aborted" ...
6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
5
by: coolsti | last post by:
Can someone tell me how to do this if it is possible? I have a table based web site, and I would like to dynamically change the text that is shown in a particular cell of a table. I give the cell...
6
by: Stefan Mueller | last post by:
The following code (HTML) generates a table. Now I'd like to insert a new row by a javascript. The following code (javascript) works with the Internet Explorer and also with Mozilla. However, the...
11
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
0
by: =?Utf-8?B?Q29uY2VwdG9y?= | last post by:
Hi all, I created a new "Component Class" in a project (Add new... & choose "Component Class"). When you get into Design Mode you see the message: "To add components to your class, drag them...
0
acoder
by: acoder | last post by:
Problem The table, when appended dynamically, does not appear on the page. Browser Internet Explorer Example The Javascript code: var obj = document.getElementById("someObjectID"); var...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.