473,387 Members | 3,033 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.

Regarding the appendChild routine to create new HTML elements.

Hi all,

I've been trying to create a table dynamically upon the generation of en event using the appendChild method in Javascript. This seems to work fine in Firefox, but not in IE7. There's also no error message from IE. I'm new to these routines. Could someone shed some light on where I could be going wrong? Here's the code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type='text/javascript'>
  4.  
  5. function makeTable()
  6. {
  7.  
  8.  
  9.    var nTable=document.createElement('table');
  10.    nTable.setAttribute('id','myTable');
  11.    nTable.setAttribute('border','1');
  12.  
  13.    var nRow1=document.createElement('tr');
  14.    var nData11=document.createElement('td');
  15.    nData11.setAttribute('colspan','2');
  16.    var nCenter11=document.createElement('center');
  17.    var nBold=document.createElement('b');
  18.    nBold.appendChild(document.createTextNode('Title'));
  19.    nCenter11.appendChild(nBold);
  20.    nData11.appendChild(nCenter11);
  21.    nRow1.appendChild(nData11);
  22.  
  23.    var nRow2=document.createElement('tr');
  24.    var nData21=document.createElement('td');
  25.    var nCenter21=document.createElement('center');
  26.   nCenter21.appendChild(document.createTextNode('21'));
  27.   nData21.appendChild(nCenter21);
  28.   var nData22=document.createElement('td');
  29.    var nCenter22=document.createElement('center');
  30.   nCenter22.appendChild(document.createTextNode('22'));
  31.   nData22.appendChild(nCenter22);
  32.   nRow2.appendChild(nData21);
  33.   nRow2.appendChild(nData22);
  34.  
  35.   nTable.appendChild(nRow1);
  36.   nTable.appendChild(nRow2);
  37.  
  38.   alert('Almost there !');
  39.   try
  40.   {
  41.        document.getElementById('container').appendChild(nTable);
  42.   }
  43.   catch (e)
  44.   {
  45.     alert(e.message);
  46.   }
  47.  
  48.  
  49.    return;
  50.  
  51. }
  52.  
  53. </script>
  54. </head>
  55.  
  56. <body>
  57. <div id='container'>
  58. </div>
  59. <br>
  60. <input type=button value='Go' onclick='makeTable();'>
  61. </body>
  62. </html>
Dec 8 '09 #1
2 2235
Frinavale
9,735 Expert Mod 8TB
Hi Vsanjit,

Welcome to Bytes.
I have moved your question to the JavaScript forum. I think you will get more help here than in the HTML forum.

PS. Please use code tags when posting code.

Good luck

-Frinny
Dec 8 '09 #2
acoder
16,027 Expert Mod 8TB
See http://bytes.com/topic/javascript/in...appear-page-ie

Append the rows to the tbody instead of the table.
Dec 11 '09 #3

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

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
1
by: Nicolas Van Lancker | last post by:
Hi folks; I have this webpage, allowing users to insert multiple records in one post into the database. Because I don't know the exact number of records they want to add, I created a little...
12
by: Keith Page | last post by:
I have a page that changes a list of checkboxes based on what city you select, however when i insert the new nodes the values are not submitted when the boxes are checked and form is sent, its like...
1
by: Ryan Stewart | last post by:
If you don't want to read this post because of its length, I understand. I've spent two and a half days on this problem and have a good deal of information to relate. And this is kind of a long...
3
by: Robi | last post by:
I have the following code: ############## var nHead=(document.getElementsByTagName)?document.getElementsByTagName("head").item(0):document.head; var nStyle=document.createElement("style"); //...
2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
1
by: Dmitry Kulinich | last post by:
var iframe = document.createElement("<IFRAME id='frame0' style='PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px' border='no' name='frame0'...
7
by: rahulragav | last post by:
Hi php-xml masters there ,warm greetings from this budding php guy..I'm new to this..and now caught up with a problem.. THE FOLLOWING is my code to extract data from a php class "simple.php" ...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.