View Full Version : JS to remove TR attributes

07-17-2010, 08:46 AM
Hi Guys,

I'm new at JS.
I need to remove TR elements from parent table but the problem is there are no table ID/Name

Is it possible to perform it?
Please see attach - i need remove red marked block... what scrip i have to use if i will put it to the green block?
Thank you.

07-17-2010, 04:30 PM
Despite the similar sounding names, Java is not the same as Javascript.
Moving from Java forum to Javascript forum.
DOM manipulation should include removal of child nodes, the JS people can give you more help with that.

Old Pedant
07-17-2010, 11:25 PM
Assuming that, indeed, there is only one

<td id="TopCell" ... >
on that page (as indeed there should be, since id's should be unique) and assuming that you want to remove the second and third <tr>s from that enclosed <table>, then *removing* the rows is easy.

*MOVING* the rows to the green-marked <div> is not so easy.

Does that <div> already contain a <table>???

If not, we would have to create a <table> inside the <div> to put the <tr> rows into.

It would help if you would show that <div>'s contents, expanded out. At least one level.


But it is clear that this page is being generated by ASP.NET. Wouldn't it be easier to change the ASP.NET code to do this, rather than trying to do it with JavaScript??

07-18-2010, 09:09 AM
Theoretically, the following code should do what you want. Unfortunately, I can't confirm that since it hasn't been tested since you didn't supply your source code nor is your request 100% clear due to your use of incorrect grammar and the issue noted by Old Pedant.

Additionally, the code would have been simpler had I been able to see the entire ID of the div element. It's only partially visible in the reference image.

var TopCell = document.getElementById("TopCell");
if (typeof TopCell.firstElementChild !== "undefined" && typeof TopCell.lastElementChild !== "undefined") {
// W3C DOM3 Core + W3C Element Traversal Method
var table_element = TopCell.firstElementChild.cloneNode(true);
var tbody_element = table_element.firstElementChild;
div_element = document.getElementById("MSOZoneCell_WebPartWPQ5").firstElementChild.firstElementChild.firstElementChild.firstElementChild.firstElementChild;
else if (typeof TopCell.getElementsByTagNameNS === "function") {
// W3C DOM3 Core Method
// Namespace‐Aware
var XHTML_NS = "http://www.w3.org/1999/xhtml";
var table_element = TopCell.getElementsByTagNameNS(XHTML_NS, "table").item(0).cloneNode(true);
var tbody_element = table_element.getElementsByTagNameNS(XHTML_NS, "tbody").item(0);
tbody_element.removeChild(tbody_element.getElementsByTagNameNS(XHTML_NS, "tr").item(0));
div_element = document.getElementById("MSOZoneCell_WebPartWPQ5").getElementsByTagNameNS(XHTML_NS, "div").item(0);
else {
// W3C DOM3 Core Method
// Not Namespace‐Aware
var table_element = TopCell.getElementsByTagName("table").item(0).cloneNode(true);
var tbody_element = table_element.getElementsByTagName("tbody").item(0);
div_element = document.getElementById("MSOZoneCell_WebPartWPQ5").getElementsByTagName("div").item(0);