Jump to content

Removechild()


johnnyg24

Recommended Posts

I am looking to remove cells from a table based on the cells name. I am currently trying to do it this way:

var table = document.getElementById(tableName).tBodies[0]	var cells = document.getElementsByName(cellName)		for(i=0;i<cells.length;i++){		table.removeChild(cells[i])	}

I get this error from FF:

Error: uncaught exception: [Exception... "Node was not found"  code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)"  location: "http://www.estout.com/scripts/projects.js Line: 431"]

I am doing something wrong but I can't figure it out. Can anyone help? Thanks.

Link to comment
Share on other sites

A parent-child relationship refers to immediate ancestry. A cell is not a child of its table. It is a child of a table row.ALSO: the correct method for removing a table cell is tableRow.deleteCell(), which you can find explained here: http://www.w3schools.com/htmldom/dom_obj_tablerow.asp . A more reliable method than shown there might be this:

function removeCell (id) {	  var myCell = documentGetElementById (id);	  myCell.parentNode.deleteCell (myCell);  }

This way, you do not need to know the id of the table row in advance. Notice too that you may end up with unpredictable rendering if you do not also give one of the remaining cells a larger colspan to compensate. Just be sure to check for the existence of a remaining cell before you try to modify it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...