Mehdi
Mehdi

Reputation: 9

How Can I Remove Specific HTML Tables?

I want to retrieve every table in the document that contains only one row and one cell. Those tables should then be entirely replaced with the contents of that single cell. For example, the below table should replaced with its text:

<table>
  <tr>
    <td>
      Sample Text
    </td>
  </tr>
</table>

converted to: Sample Text

Can any of html editors such as MS Expression Web or DreamWeaver help for this or do I have to write a program? I have some experience in C#.

Upvotes: 0

Views: 746

Answers (1)

Gary
Gary

Reputation: 13912

This will find every table with 1 row and 1 cell and replace it with its inner text.

var allTables = document.getElementsByTagName("table");
for(a=0; a<allTables.length; a++){
var allRows = allTables[a].getElementsByTagName("tr");
var allCells = allRows[0].getElementsByTagName("td");
  if(allRows.length===1 && allCells.length===1){
    var tdInnards = document.createElement("div");
        tdInnards.appendChild(document.createTextNode(allCells[0].innerHTML));
    }
  }
  allTables[a].insertBefore(tdInnards);
  allTables[a].parentNode.removeChild(allTables[a]);
}

However, this is only JavaScript so it will aesthetically accomplish what you want, but won't actually rewrite the page. You could perhaps use something similar to parse the text and output the result.

Upvotes: 1

Related Questions