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

DOM to create <TR> & <TD>

Hi there,

I'm trying dynamically generate extra rows in a table. So far this achieves
this task within the tHead segment:

theTable.tHead.appendChild(document.createElement( 'TR'))

....but this only gives me table rows with no table data cells. So I would
like to know if it's possible to create several <TH> within the tHead row
using this method?

I'm new to DOM manipulation so any information or pointers would be greatly
appreciated.

- Thanks.
Jul 20 '05 #1
4 7201
El Diablo wrote:
Hi there,

I'm trying dynamically generate extra rows in a table. So far this achieves
this task within the tHead segment:

theTable.tHead.appendChild(document.createElement( 'TR'))

....but this only gives me table rows with no table data cells. So I would
like to know if it's possible to create several <TH> within the tHead row
using this method?

Can't you just use the same methodology?
Create a TH (or a TD) element & append it to the TR (as many as are
required). Since the TR is already appended to the thead y'r ok.

GL
Fermin DCG
Jul 20 '05 #2


El Diablo wrote:

I'm trying dynamically generate extra rows in a table. So far this achieves
this task within the tHead segment:

theTable.tHead.appendChild(document.createElement( 'TR'))

...but this only gives me table rows with no table data cells. So I would
like to know if it's possible to create several <TH> within the tHead row
using this method?

I'm new to DOM manipulation so any information or pointers would be greatly
appreciated.


Well, once you know that
document.createElement('tagname')
creates an element object with that tagname and you know appendChild it
should be clear that you can store an element object reference in a
variable, append child nodes as follows:
var row = document.createElement('tr');
var cell = document.createElement('td');
cell.appendChild(document.createTextNode('Kibology .'));
row.appendChild(cell);
cell = document.createElement('td');
cell.appendChild(document.createTextNode('JavaScri pt'));
row.appendChild(cell);
...
Then when you are done creating table cells use appendChild on a table
or table section to append the row.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #3
Exactly what I needed to know. Many thanks.

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:40******@olaf.komtel.net...


El Diablo wrote:

I'm trying dynamically generate extra rows in a table. So far this achieves this task within the tHead segment:

theTable.tHead.appendChild(document.createElement( 'TR'))

...but this only gives me table rows with no table data cells. So I would like to know if it's possible to create several <TH> within the tHead row using this method?

I'm new to DOM manipulation so any information or pointers would be greatly appreciated.


Well, once you know that
document.createElement('tagname')
creates an element object with that tagname and you know appendChild it
should be clear that you can store an element object reference in a
variable, append child nodes as follows:
var row = document.createElement('tr');
var cell = document.createElement('td');
cell.appendChild(document.createTextNode('Kibology .'));
row.appendChild(cell);
cell = document.createElement('td');
cell.appendChild(document.createTextNode('JavaScri pt'));
row.appendChild(cell);
...
Then when you are done creating table cells use appendChild on a table
or table section to append the row.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4
Hi.
I've got an example.
Here's the code:

<html>
<head>
<title>Example</title>
<script language="javascript">

function Remove(){
var tbody = document.getElementById("tbl").firstChild;
if(tbody.childNodes.length <= 1)
{
alert('There are no regists!');
return;
}

var tr = tbody.lastChild;

tbody.removeChild(tr);
}

function Insere(){
var tbody=document.getElementById("tbl").firstChild;
var newTR = document.createElement("tr");
var newTD1 = document.createElement("td");
var newTD2 = document.createElement("td");
var newText1 = document.createTextNode(document.cart.primNome.val ue);
var newText2 = document.createTextNode(document.cart.ultNome.valu e);

newTD1.appendChild(newText1);
newTD2.appendChild(newText2);
newTR.appendChild(newTD1);
newTR.appendChild(newTD2);
tbody.appendChild(newTR);
}
</script>
</head>
<body id="bodyTag">
<button onclick="Remove()" id=button1 name=button1>Remover</button>
<button onclick="Insere()" id=button2 name=button2>Adicionar</button>
<form id="cart" name=cart>
1º Nome: <input type=text id=primNome name="primNome">
<br>Último nome: <input type=text id=ultNome name="ultNome">
</form>
<table id=tbl bgcolor=#ffffff>
<tr bgcolor=#ccccff>
<th>1º Nome</td>
<th>Último nome</td>
</tr>
</table>
</body>
</html>

There are words in Portuguese sorry but i don't want to change.
This simple form accepts the first and the last name, and then creates a
<tr> with 2 <tr> for show the values.
Hope this helps.
"El Diablo" <ze*****@zen.co.uk> escreveu na mensagem
news:40**********************@lovejoy.zen.co.uk...
Hi there,

I'm trying dynamically generate extra rows in a table. So far this achieves this task within the tHead segment:

theTable.tHead.appendChild(document.createElement( 'TR'))

...but this only gives me table rows with no table data cells. So I would
like to know if it's possible to create several <TH> within the tHead row
using this method?

I'm new to DOM manipulation so any information or pointers would be greatly appreciated.

- Thanks.

Jul 20 '05 #5

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

Similar topics

0
by: Matt Adams | last post by:
I want to move the following <PRE> defintion to a css file: <TABLE><TR><TD><PRE>sample text</PRE></TD> <TD> not predefined font</TD></TR></TABLE> should be <TABLE><TR><TD class=aaa>sample...
5
by: Monty | last post by:
..rowbackgrnd { background-color: #E4E4F2; border-bottom: 1px solid Black; border-left: 1px solid #E4E4F2; border-right: 1px solid #E4E4F2; border-top: 1px solid Black } It works for <td>...
2
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a...
5
by: Gregor Rot | last post by:
Hi, if i have this code: <form><input type=text name="id1"></form> then with javascript i can reference this with this.form.id1, how can i reference something like this: <table> <tr...
2
by: bissatch | last post by:
Hi, I am trying to use JavaScript to write a table column on a web page. The code is as follows: <html> <head> <script> function displaycount() {
3
by: RC | last post by:
I have a very sime html table like <html><head><title>My Table</title> <style> input { margin: 0; padding: 0; border-width: 0; text-indet: 0; text-align: left } </style></head><body> <table...
28
by: entfred | last post by:
I have the following line of html: &nbsp;&nbsp1234&nbsp;&nbsp;&nbsp;&nbsp;&nbspabc&nbsp;&nbsp;&nbspyow In Internet Explorer 6.0, the columns look ok using the above html: 1234 abcd ...
4
George Lft
by: George Lft | last post by:
I'm new at building table. Mostly PHP programming . Now I can't seem to fix the size of my table row and column. Any idea? <h4>Two rows and three columns:</h4> <table border="1"> <tr> ...
1
by: test9991014 | last post by:
Hi folks, I've got something like this: <table> <tr> <td>1</td> <td align=center> <input type=text> </td>
7
by: Xiaoyan | last post by:
Hi,everyone: I have a problem now. I can't get the information between the <tr><td> and </td></tr>. for example: I use this regular expression can't get it, I don't know why....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
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?
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...

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.