appendChild, invalid child type?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Sep 2009
Posts: 11
Reputation: Ghodmode is an unknown quantity at this point 
Solved Threads: 3
Ghodmode's Avatar
Ghodmode Ghodmode is offline Offline
Newbie Poster

appendChild, invalid child type?

 
0
  #1
Oct 26th, 2009
I'd like to use JavaScript to add rows to a table. It seems like an easy idea, but it occurs to me that the parent node of the rows might technically be a TBODY element. So, I'm wondering if I use appendChild to add a table row to a table element am I doing something wrong and, more importantly, is it going to fail in browser x?

For example:
  1. function addTheRow() {
  2. var myTable = document.getElementById('myTable');
  3. var newRow = document.createElement('TR');
  4. var newCell = document.createElement('TD');
  5. newCell.appendChild( document.createTextNode('Yeah, Baby! Yeah!!') );
  6. newRow.appendChild( newCell );
  7.  
  8. myTable.appendChild( newRow );
  9. } // End addTheRow function

Should I do something to identify the TBODY element and add the element to that instead?

Thank you.

--
-- Ghodmode
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 180
Reputation: codejoust is an unknown quantity at this point 
Solved Threads: 20
codejoust's Avatar
codejoust codejoust is offline Offline
Junior Poster
 
1
  #2
Oct 26th, 2009
Do-- document.getElementById('myTable').getElementsByTagName('tbody')[0] to get the tbody instead of the whole table if you want to.
Typically, browsers will try to 'fix' your markup by automatically inserting tbody.
Last edited by peter_budo; Oct 27th, 2009 at 10:24 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 408 | Replies: 1
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC