Connecting Tech Pros Worldwide Help | Site Map

Using DOM to insert a DIV within a TD

Member
 
Join Date: Feb 2008
Posts: 123
#1: Jul 22 '09
Hi all,

I am dynamically creating a DIV within a newly added row to a previously created table. I'm stuck with the correct syntax. Below is my current effort:

Expand|Select|Wrap|Line Numbers
  1. function gradType(obj) {
  2. insertDiv = document.createElement("div");
  3.                 if (obj=="0") {
  4. insertdiv = document.getElementById("insert");
  5. while(insertDiv.firstChild)
  6. {
  7. insertDiv.removeChild(insertDiv.firstChild);
  8. }
  9.                return; 
  10.         } else if (obj=="1") {
  11. //add row to table and populate with inputs
  12. cnt++;
  13. var table = document.getElementById("yourtable");
  14. var rowCount = table.rows.length;  
  15. var addRow = table.insertRow(rowCount);
  16. addRow.id = cnt-1;
  17. var addCell = addRow.insertCell(0);
  18. addCell.colSpan = 3;
  19. addCell.innerHTML = '<div id="insert">Compund:<input name="Xcomp[]" id="alt" type="text"></div>';
  20.  
  21. addCell.appendChild(insertDiv);
  22.  
  23.                 return;
  24.         }
  25. }
  26.  
Thanks in advance for any help!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jul 22 '09

re: Using DOM to insert a DIV within a TD


insertDiv on lines 5 and 7 should be insertdiv (you should probably use another name to avoid confusion). Move line 2 to where you want to create the div (unless you're happy with innerHTML).
Member
 
Join Date: Feb 2008
Posts: 123
#3: Jul 22 '09

re: Using DOM to insert a DIV within a TD


Thanks for the reply but I'm still stuck. I'm basically trying to insert cells into an existing table, and place them within a DIV so these new cells can be easily removed later. Below is more succinct code which is supposed to add a row of 2 cells to a table that already has cells, and keep the new cells within a single DIV:

Expand|Select|Wrap|Line Numbers
  1. var table = document.getElementById("yourtable");
  2. var rowCount = table.rows.length;  
  3. var addRow = table.insertRow(rowCount);
  4. insertDiv = document.createElement("div");
  5. insertDiv.id = "insertedDIV";
  6. var addCell = addRow.insertCell(0);
  7. addCell.innerHTML = 'HELLO';
  8.  
  9. var addCell = addRow.insertCell(1);
  10. addCell.innerHTML = 'GOODBYE';
  11.  
  12. //Now color just the new cells - NOT all cells within the table
  13. insertDiv.style.background='rgb(128, 128, 128)';
  14.  
Thanks!
Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#4: Jul 22 '09

re: Using DOM to insert a DIV within a TD


You cannot place cells inside a DIV, if you want to quickly remove them later, remove the row. Plus in the code, I can't see any usage of the DIV you created.
Member
 
Join Date: Feb 2008
Posts: 123
#5: Jul 23 '09

re: Using DOM to insert a DIV within a TD


Thanks for the reply.

Yes, I am confused as to how to start a DIV, insert nodes(?) such as cells into the DIV, and then close the DIV.

So as to help me better understand the subtleties of the DOM, could someone adapt the above code so the contents of just one cell is placed inside the new DIV? Or, give a link to a page that has a similar example (I can't find anything thru Google).

Thanks!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Jul 23 '09

re: Using DOM to insert a DIV within a TD


wouldn’t it be easier to pass a class or id to the newly created cells to get rid of them easier?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Jul 23 '09

re: Using DOM to insert a DIV within a TD


As Canabeez mentioned, you can't insert a table cell inside a div - well, you could try with innerHTML, but it'd be incorrect HTML and you can't be certain what to expect.

If the only reason you want to create a div is to be able to access it later to remove its contents, then give the cell a unique ID instead of creating a div.

As for your question about closing the div, that's done automatically. document.createElement creates a whole element/object which you can add properties to - don't think in terms of HTML strings.
Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#8: Jul 23 '09

re: Using DOM to insert a DIV within a TD


In addition to acoder.

You could also use the "name" parameter for the cells. Name all the cells you'd like to delete later with a same name like myCell, and then just:

Expand|Select|Wrap|Line Numbers
  1. function deleteCells()
  2. {
  3. var myCells = document.getElementsByName('myCell');
  4. for(var i=0;i<meCells.length;i++){myCells[i].parentNode.removeChild(myCells[i]);}
  5. }
  6.  
Didn't test it, but I think this should work fine ;)

Good luck
Member
 
Join Date: Feb 2008
Posts: 123
#9: Jul 24 '09

re: Using DOM to insert a DIV within a TD


Thanks for the the replies!

I'm working out a way to keep track of inserts by giving parent rows IDs, and deleting their daughters automatically. Its a lot more complicated than using DIVs, but the code is now *almost* working.

Thanks again to all those who responded.
Member
 
Join Date: Feb 2008
Posts: 123
#10: Jul 24 '09

re: Using DOM to insert a DIV within a TD


Hi again,

A quick follow-up question:

I have a routine that creates "parent" rows in a table and assigns them a unique row ID. I then have a sub-routine that can insert multiple "daughter" rows in between "parent" rows that don't have row IDs. I have successfully figured out how to determine a selected "parent" row by using:

parentRowID = element.parentNode.parentNode.id;

I now want to check if the next row down has a row ID assigned (i.e., is the next row down a "daughter" row?).

Any suggestions on how to do this would be great!

Thanks
Member
 
Join Date: Feb 2008
Posts: 123
#11: Jul 25 '09

re: Using DOM to insert a DIV within a TD


I've figured it out...

nextSibling
Reply


Similar JavaScript / Ajax / DHTML bytes