Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2003, 11:27 PM   PM User | #1
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
Cannot append table

Can someone tell me why I can append the DOM table to any of the DIV's but cannot append the DOM table between table one and two







<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<Script>
<!--
function addTable(){
mytable=document.createElement("TABLE");
mytable.setAttribute("border","2")

t_body=document.createElement("TBODY");

for(r=0;r<1;r++){
newrow=document.createElement("TR");

for(c=0;c<1;c++){
newcell = document.createElement("TD");
new_text=document.createTextNode("Table 3");
newcell.appendChild(new_text);
newrow.appendChild(newcell);
}

t_body.appendChild(newrow);
}

mytable.appendChild(t_body);
//doc_body=document.getElementsByTagName("DIV").item(3) // append a DIV
doc_body=document.getElementsByTagName("TABLE").item(0)
doc_body.appendChild(mytable);
}
// -->
</script>

</HEAD>
<BODY onload="addTable()">
<form name="f1">
<TABLE border="1"><TBODY><tr><td>Table</td><td>One</td</tr></TBODY></TABLE>
<TABLE border="1"><TBODY><tr><td>Table</td><td>Two</td></tr></TBODY></TABLE>

<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</BODY>
</HTML>
Mr J is offline   Reply With Quote
Old 10-14-2003, 01:34 AM   PM User | #2
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
You're appending the created table to the first table. Try changing this:

Code:
//doc_body=document.getElementsByTagName("DIV").item(3) // append a DIV
doc_body=document.getElementsByTagName("TABLE").item(0)
doc_body.appendChild(mytable);
...to this:

Code:
var doc_body = document.getElementsByTagName("table").item(1);
var div = doc_body.parentNode;
div.insertBefore(myTable, doc_body);
I *think* that will work.

Hope that helps!

Happy coding!
nolachrymose is offline   Reply With Quote
Old 10-14-2003, 05:50 PM   PM User | #3
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
Thank you, your solution does work.

I am just learning the DOM scripting so I am puzzled as to why appendChild() will append my table to any other tag I choose but not a table tag
Mr J is offline   Reply With Quote
Old 10-14-2003, 06:13 PM   PM User | #4
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Bacause a table element can't be a child of a table element? Or perhaps it appends it but the browser doesn't render it?
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 07-31-2007, 05:22 PM   PM User | #5
taicho2827
New to the CF scene

 
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
taicho2827 is an unknown quantity at this point
Well to answer your question every table MUST have a <tbody> tag. If it is not explicitly defined it will implicitly create one for you. Here is a little javascript snippet that will help you with your coding.

function createTable()
{
// create the new table tag
var table = document.createElement("table");

// create the new table body tag
var tbody = document.createElement("tbody");

// create the new table row tag
var tr = document.createElement("tr");

// create the new table definition tag
var td = document.createElement("td");

// create the new tag of your choice to go into the tag.
var text = document.createElement("textarea");


// create the outer tr to go around the newly created table
var OuterTR = document.createElement("tr");

// create the outer td to go around the newly created table
var OuterTD = document.createElement("td");

// append tags accordingly.
td.appendChild(text);
tr.appendChild(td);
tbody.appendChild(tr);
table.appendChild(tbody);
OuterTD.appendChild(table);
OuterTR.appendChild(OuterTD);


var myTable = document.getElementById("tbody");
myTable.appendChild(OuterTR);

}

Then all you have to have is a table described in your html. IE:

<html>
<head>
</head>

<body>
<table>
<tbody id='tbody'>
</tbody>
</table>
</body>

</html>
taicho2827 is offline   Reply With Quote
Old 07-31-2007, 09:36 PM   PM User | #6
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
taicho2827

This post is four years old mate
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 08-01-2007, 12:53 AM   PM User | #7
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,906
Thanks: 5
Thanked 189 Times in 186 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by taicho2827 View Post
Well to answer your question every table MUST have a <tbody> tag.
The correct terminology is tbody element. You don’t manipulate tags except when using things like document.write or innerHTML.

Quote:
Originally Posted by taicho2827 View Post
Then all you have to have is a table described in your html. IE:

<html>
<head>
</head>

<body>
<table>
<tbody id='tbody'>
</tbody>
</table>
</body>

</html>
This document is invalid. The outer elements should be created via scripting too.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:50 PM.


Advertisement
Log in to turn off these ads.