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

Modifying HTML Table through DOM

The basic concept is a music playlist that I'm trying to write as a
table inside a div.

I have Song and Playlist 'classes'. The Song class has a Write()
method, as does the Playlist.

The Song.Write() boils down to this:
var tr = document.createElement("TR");
var td = document.createElement("TD");
tr.id = this.ID;
tr.appendChild(td);
td.innerText = this.Artist + ' - ' + this.Title;
td.onclick = function() {top.playSong(this.parentElement.id);}
return tr;

The Playlist.Write() call is essentially this:
// this.songs is an Array of Song objects
for (var x = 0; x < this.songs.length; x++) {
playlistTable.appendChild(this.songs[x].Write());
}

When I attempt to call playlistTable.appendChild() after creating the
table via document.createElement("Table"), everything works fine,
except I have no way to add the table to my document, since the div I
want it in won't believe that appendChild() is a valid method. If I
put the table in the div as a <TABLE>, and then try to append to that,
the *table* doesn't accept appendChild() as valid. Specifically, I get
an 'Invalid argument' error when calling either.

If anyone has ideas on what I'm missing, or where I screwed up, I
would be most grateful. :)

TIA

~ Jeff
Jul 20 '05 #1
3 9674
In article <f2**************************@posting.google.com >, jpj625
@earthlink.net enlightened us with...

When I attempt to call playlistTable.appendChild() after creating the
table via document.createElement("Table"), everything works fine,
except I have no way to add the table to my document, since the div I
want it in won't believe that appendChild() is a valid method.
Something's wrong. It should. Full code online anywhere?
If I
put the table in the div as a <TABLE>, and then try to append to that,
the *table* doesn't accept appendChild() as valid. Specifically, I get
an 'Invalid argument' error when calling either.


You must have TBODY and append to TBODY. I spent a few hours on that
issue once. :)

Sounds like you are calling appendChild on the wrong object. Without
seeing your full code to be sure, this
playlistTable.appendChild(this.songs[x].Write());
seems invalid. You're appending a TR to a TABLE instead of a TBODY
element, I think. Instead...

<table id="playlistTable">
<tbody id="playlistTableBody">
....
</table>

document.getElementById("playlistTableBody").appen dChild(this.songs
[x].Write());
-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #2
I *think* (I read in a newsgroup) that some browsers don't
like this way of building tables. Don't know if it's true and
I couldn't cite a source (though this newsgroup is a likely
place), but I've always built and modified them using
..insertRow and .insertCell

Good luck,
Csaba Gabor

"Jeff J." <jp****@earthlink.net> wrote in message news:f2**************************@posting.google.c om...
The basic concept is a music playlist that I'm trying to write as a
table inside a div.

I have Song and Playlist 'classes'. The Song class has a Write()
method, as does the Playlist.

The Song.Write() boils down to this:
var tr = document.createElement("TR");
var td = document.createElement("TD");
tr.id = this.ID;
tr.appendChild(td);
td.innerText = this.Artist + ' - ' + this.Title;
td.onclick = function() {top.playSong(this.parentElement.id);}
return tr;

The Playlist.Write() call is essentially this:
// this.songs is an Array of Song objects
for (var x = 0; x < this.songs.length; x++) {
playlistTable.appendChild(this.songs[x].Write());
}

When I attempt to call playlistTable.appendChild() after creating the
table via document.createElement("Table"), everything works fine,
except I have no way to add the table to my document, since the div I
want it in won't believe that appendChild() is a valid method. If I
put the table in the div as a <TABLE>, and then try to append to that,
the *table* doesn't accept appendChild() as valid. Specifically, I get
an 'Invalid argument' error when calling either.

If anyone has ideas on what I'm missing, or where I screwed up, I
would be most grateful. :)

TIA

~ Jeff

Jul 20 '05 #3
In article <f2**************************@posting.google.com >, jpj625
@earthlink.net enlightened us with...

I now get an "Unspecified error" when attempting to append to the
TBODY.


Well, I've been working on some code to make dynamic tables, and it
works fine with the target being a DIV. So I have no clue what you're
doing wrong, since I can't see all your code. You should make a test
file that demonstrates the problem so people can copy and paste and play
with it.

I notice you use innerText - I do not. I use createTextNode. Maybe
that's your problem? I dunno. Also note your onclick should have
attachEvent for compatible browsers
(from one of my pages)
if (S.attachEvent)
{
S.attachEvent('onchange', buildForm1);
}
else
{
S.onchange = buildForm1;
}

Here's what I have so far for my generic DHTML table. The style stuff
doesn't work in IE but does work in NN (see one of my posts in this
group LOL).
The table creates fine whether target is the div or document.body.
See if your code looks like mine. Use this code if you want, I don't
care.

<html>
<head>
<title>Javascript test - dynamic elements</title>

<script type="text/javascript" language="javascript">
function createTable(tableId, appendElement)
{
// table attributes as constants for easy modification
STYLE="border: thin solid navy;";

// create table and tbody
T = document.createElement("table");
TB = document.createElement("tbody");

// set table and tbody attributes
T.setAttribute("id",tableId);
T.setAttribute("name",tableId);
T.setAttribute("style",STYLE);
TB.setAttribute("id",tableId+"_body");
TB.setAttribute("name",tableId+"_body");

// append elements
T.appendChild(TB);
appendElement.appendChild(T);
}

function createRow(rowId, appendElement)
{
// row attributes as constants for easy modification
STYLE="background-color:yellow";

// create row
R = document.createElement("tr");

// set attributes
R.setAttribute("id",rowId);
R.setAttribute("name",rowId);
R.setAttribute("style",STYLE);

// append element
appendElement.appendChild(R);
}

function createCell(cellId, appendElement)
{
// cell attributes as constants for easy modification
STYLE="color: navy";

// create cell
C = document.createElement("td");

// set attributes
C.setAttribute("id",cellId);
C.setAttribute("name",cellId);
C.setAttribute("style",STYLE);

// append element
appendElement.appendChild(C);
}

function appendCellText(appendElement, text)
{
// create text node
T = document.createTextNode(text);

// append element
appendElement.appendChild(T);
}

function createDiv(divId, appendElement)
{
STYLE="border: thin solid navy; width: 300px; height: 300px;";

// create
D = document.createElement("div");

// set attributes
D.setAttribute("id",divId);
D.setAttribute("style",STYLE);

// append element
appendElement.appendChild(D);
}

function testRun()
{
createTable('newTable',document.getElementById('ta rget'));
createRow('newRow',document.getElementById('newTab le_body'));
createCell('newCell',document.getElementById('newR ow'));
appendCellText(document.getElementById('newCell'), 'This is a cell');
createDiv("div1",document.body);
}
</script>
</head>

<body>
<form name="f1">
<p>
<input type="button" name="b1" value="test it" onClick="testRun()">
</p>
</form>
<div id="target"></div>
</body>
</html>
-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #4

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

Similar topics

2
by: Kirk Is | last post by:
Hi there. I'm trying to improve a simple javascript based graphics editor for Atari 2600 games (long story) with better use of CSS and Javascript. I'm a little weak on what CSS can and can't...
3
by: Chris | last post by:
I have a dataset that I want to modify the values in a particular column. I.e if I have a function to convert the enterbyID (Integer) to a name, in the dataset the field (Concern_EnteredbyID) shows...
2
by: Együd Csaba | last post by:
Hi All, I'm wonder if there is any possibility to modify a users password by updating the pg_shadow table. I'd like to ensure that when the user modifies his/her http password (htpasswd) than...
2
by: rk | last post by:
I have the following library.xml file coming from a system, this can't be modified. ____________________________________________________________________________ <?xml version="1.0"...
6
by: Welie | last post by:
This is sort of a newbie question. I have an access database application. It is split into a backend data file and GUI. I need to make changes to the db schema. The production version of the...
2
by: rob.nikander | last post by:
Hi, I'm working on a web page that uses the DOM API to move elements within the page in response to an onclick event. My problem is that in Firefox (but not in Safari and IE) I can see...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
1
by: kcddoorman | last post by:
I have searched all over the internets for a solution to this problem and there are many ways to work around it but I'm wondering if there is a more simple solution. I have asp.net 2.0 working with...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
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...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.