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

Creating Dynamic Tables with JavaScript

Hello,

I'm new to Javascript, but I'm trying to create html tables based off a checkbox action. Is it possible to do this on the same page?

I've noticed that document.write() will create the tables in a new request which isn't what I'm looking for. I attempted to do this via DOM but I can't get the tables to show up in the page.
Expand|Select|Wrap|Line Numbers
  1.                 function checkboxOnDemand(obj, parentId, childId, topHeader)
  2.     {
  3.         var checkbox = eval("document.checkForm." + obj.name);
  4.  
  5.         if(checkbox.checked == true)
  6.         {
  7.             alert("Adding table");
  8.             addParentTable(parentId, childId, topHeader);
  9.         }
  10.         else
  11.         {
  12.             alert("Removing table");
  13.             removeElement(parentId, childId);
  14.         }
  15.     }
  16.     function addParentTable(parentId, childId, topHeader)
  17.     {
  18.         // get parent element (div)
  19.         var element = document.getElementById(parentId);
  20.  
  21.         if(element != null)
  22.         {
  23.             // create parent table 
  24.             var table = document.createElement('table');
  25.             table.setAttribute('name', childId);
  26.  
  27.             // add style to table
  28.             var cssString = 'border:1px solid #000;width:600;left:800';
  29.             //table.style.cssText = cssString;
  30.             table.setAttribute('style',cssString);
  31.  
  32.             // add table to parent
  33.             element.appendChild(table);
  34.  
  35.             // create header row 1
  36.             var tr1 = document.createElement('tr');
  37.             var td1 = document.createElement('td');
  38.  
  39.             table.appendChild(tr1);
  40.             tr1.appendChild(td1);
  41.  
  42.             // add style to table data
  43.             var cssString1 = 'font-weight:bold;height:10';
  44.             //td.style.cssText = cssString1;
  45.             td1.setAttribute('style',cssString1);
  46.  
  47.             //var text1 = document.createTextNode(topHeader);
  48.             //td.appendChild(text1);
  49.  
  50.             for(var y = 1;y <= rowHeader.length;y++) 
  51.             {
  52.                 var tr = document.createElement('tr');
  53.                 var td = document.createElement('td');
  54.  
  55.                 // add table elements
  56.                 table.appendChild(tr);
  57.                 tr.appendChild(td);
  58.  
  59.                 // add style to table data
  60.                 var cssString = 'width:300';
  61.                 //td.style.cssText = cssString;
  62.                 td.setAttribute('style', cssString);
  63.  
  64.                 var div = document.createElement('div');
  65.                 div.setAttribute('class', 'labelHeader');
  66.  
  67.                 var text2 = document.createTextNode(rowHeader[y-1]);
  68.                 div.appendChild(text2);
  69.  
  70.                 var td2 = document.createElement('td');
  71.                 var cssString2 = 'width:300';
  72.                 //td2.style.cssText = cssString2;
  73.                 td2.setAttribute('style', cssString2);
  74.                 tr.appendChild(td2);
  75.  
  76.                 var textBox = document.createElement('input');
  77.  
  78.                 if(y == 3)
  79.                 {
  80.                     textBox.setAttribute('type', 'file');
  81.  
  82.                     // add "add more files" button here
  83.                     var fileButton = document.createElement('input');
  84.                     fileButton.setAttribute('type', 'button');
  85.                     fileButton.setAttribute('href', '#');
  86.                     fileButton.setAttribute('onClick', '\addAdditionalTable();\"');
  87.                 }
  88.                 else
  89.                 {
  90.                     textBox.setAttribute('type', 'text');
  91.                 }
  92.  
  93.                 textBox.setAttribute('size', 30);
  94.                 textBox.setAttribute('name', ('element' + y));
  95.                 td2.appendChild(textBox);            
  96.             }
  97.  
  98.             /*var br1 = document.createElement('br');
  99.             var br2 = document.createElement('br');
  100.             element.appendChild(br1);
  101.             element.appendChild(br2);*/
  102.         }
  103.     }
  104.  
  105. <!-- JSP code that creates the checkboxes -->
  106. <table height=100px width=300px>
  107. <tr>
  108. <td style="width:150"> <div class="labelHeader">Field Name:</div>
  109. </td>
  110. <td class="scroll" style="border:1px solid #000;width:200px; height:150px; overflow: auto">
  111. <% 
  112. for(int x=1;x<=headers.length;x++) 
  113. {
  114. out.println("<input type=\"checkbox\" onClick=\"checkboxOnDemand(this, \'tableSpace\', \'" + tables[x-1] 
  115. + "\', \'" + headers[x-1] + "\');\" name=\"checkBox" + x + "\" />" + headers[x-1] + "<br>");
  116. }%>    
  117. </td>
  118. </tr>
  119. </table>
Jun 5 '08 #1
2 2638
vikas251074
198 100+
So this is your code
OK

Expand|Select|Wrap|Line Numbers
  1. function checkboxOnDemand(obj, parentId, childId, topHeader)
  2. {
  3. var checkbox = eval("document.checkForm." + obj.name);
  4.  
  5. if(checkbox.checked == true)
  6. {
  7. alert("Adding table");
  8. addParentTable(parentId, childId, topHeader);
  9. }
  10. else
  11. {
  12. alert("Removing table");
  13. removeElement(parentId, childId);
  14. }
  15. }
  16. function addParentTable(parentId, childId, topHeader)
  17. {
  18. // get parent element (div)
  19. var element = document.getElementById(parentId);
  20.  
  21. if(element != null)
  22. {
  23. // create parent table 
  24. var table = document.createElement('table');
  25. table.setAttribute('name', childId);
  26.  
  27. // add style to table
  28. var cssString = 'border:1px solid #000;width:600;left:800';
  29. //table.style.cssText = cssString;
  30. table.setAttribute('style',cssString);
  31.  
  32. // add table to parent
  33. element.appendChild(table);
  34.  
  35. // create header row 1
  36. var tr1 = document.createElement('tr');
  37. var td1 = document.createElement('td');
  38.  
  39. table.appendChild(tr1);
  40. tr1.appendChild(td1);
  41.  
  42. // add style to table data
  43. var cssString1 = 'font-weight:bold;height:10';
  44. //td.style.cssText = cssString1;
  45. td1.setAttribute('style',cssString1);
  46.  
  47. //var text1 = document.createTextNode(topHeader);
  48. //td.appendChild(text1);
  49.  
  50. for(var y = 1;y <= rowHeader.length;y++) 
  51. {
  52. var tr = document.createElement('tr');
  53. var td = document.createElement('td');
  54.  
  55. // add table elements
  56. table.appendChild(tr);
  57. tr.appendChild(td);
  58.  
  59. // add style to table data
  60. var cssString = 'width:300';
  61. //td.style.cssText = cssString;
  62. td.setAttribute('style', cssString);
  63.  
  64. var div = document.createElement('div');
  65. div.setAttribute('class', 'labelHeader');
  66.  
  67. var text2 = document.createTextNode(rowHeader[y-1]);
  68. div.appendChild(text2);
  69.  
  70. var td2 = document.createElement('td');
  71. var cssString2 = 'width:300';
  72. //td2.style.cssText = cssString2;
  73. td2.setAttribute('style', cssString2);
  74. tr.appendChild(td2);
  75.  
  76. var textBox = document.createElement('input');
  77.  
  78. if(y == 3)
  79. {
  80. textBox.setAttribute('type', 'file');
  81.  
  82. // add "add more files" button here
  83. var fileButton = document.createElement('input');
  84. fileButton.setAttribute('type', 'button');
  85. fileButton.setAttribute('href', '#');
  86. fileButton.setAttribute('onClick', '\addAdditionalTable();\"');
  87. }
  88. else
  89. {
  90. textBox.setAttribute('type', 'text');
  91. }
  92.  
  93. textBox.setAttribute('size', 30);
  94. textBox.setAttribute('name', ('element' + y));
  95. td2.appendChild(textBox); 
  96. }
  97.  
  98. /*var br1 = document.createElement('br');
  99. var br2 = document.createElement('br');
  100. element.appendChild(br1);
  101. element.appendChild(br2);*/
  102. }
  103. }
  104.  
  105. <!-- JSP code that creates the checkboxes -->
  106. <table height=100px width=300px>
  107. <tr>
  108. <td style="width:150"> <div class="labelHeader">Field Name:</div>
  109. </td>
  110. <td class="scroll" style="border:1px solid #000;width:200px; height:150px; overflow: auto">
  111. <% 
  112. for(int x=1;x<=headers.length;x++) 
  113. {
  114. out.println("<input type=\"checkbox\" onClick=\"checkboxOnDemand(this, \'tableSpace\', \'" + tables[x-1] 
  115. + "\', \'" + headers[x-1] + "\');\" name=\"checkBox" + x + "\" />" + headers[x-1] + "<br>");
  116. }%> 
  117. </td>
  118. </tr>
  119. </table> 
Jun 6 '08 #2
acoder
16,027 Expert Mod 8TB
You need to add it to a tbody in IE (I assume the problem is in IE).

See Browser Quirk: Dynamically appended table does not appear on page.
Jun 6 '08 #3

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

Similar topics

5
by: Billy Cormic | last post by:
Hello, I am interested in dynamically creating temp tables using a variable in MS SQL Server 2000. For example: DECLARE @l_personsUID int select @l_personsUID = 9842
3
by: Craig Jurney | last post by:
Am having difficulty creating a dynamic <select> element using direct assignment to the element's option array (ie. someElement.option=new Option(someText, someValue);) that will work on Palm...
7
by: Stan Sainte-Rose | last post by:
Hi, I use this bit of code to generate dynamic tables in the page load section .... Dim ntable as New Table For i = 1993 To 2008 ntable = New Table ntable.ID = "Q" + i.ToString .... ....
1
by: mht7 | last post by:
I'm newbie to javascript and I did an extensive search on this site and couple of others looking for directions. I'm attempting to write some custom javascript for collapsing the tables and fit it...
16
by: pukivruki | last post by:
hi, I wish to create a temporary table who's name is dynamic based on the argument. ALTER PROCEDURE . @PID1 VARCHAR(50), @PID2 VARCHAR(50), @TICKET VARCHAR(20)
3
by: kpmassey | last post by:
I'm using javascript to construct large tables from an array of data, using ideas from: http://www.oreillynet.com/pub/a/javascript/2003/05/06/dannygoodman.html The data itself cannot be...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
1
by: skyson2ye | last post by:
Hi, guys: I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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.