-1

I am having a table like:

<table id="toc" class="toc" border="1" summary="Contents">
</table>

in many pages. All these pages are rendered in a single page. When I apply the javascript to delete that using on load with the below:

var tbl = document.getElementById('toc');
if(tbl) tbl.parentNode.removeChild(tbl);

Only one table is deleted and not the others. I am trying to delete the tables in all the rendering pages using javascript. How to do this?

Edit : I myself found the solution

 <script type='text/javascript'>
    window.onLoad = load(); 
  function load(){var tbl = document.getElementById('toc'); 
   if(tbl) tbl.parentNode.removeChild(tbl);} 
 </script> 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
useranon
  • 29,318
  • 31
  • 98
  • 146

1 Answers1

2

An ID is a unique identifier, which basically means: There can be only one.

Try looking up by tagname (table) instead and comparing the classname.

var allTables = document.getElementsByTagName('table');

for (var i = 0; i < allTables.length; i++) {
    // Array.indexOf may not be available, see
    // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf

    if (allTables[i].className.split(' ').indexOf('toc') != -1) {
        var node = allTables[i];
        node.parentNode.removeChild(node);
    }
}
nikc.org
  • 16,462
  • 6
  • 50
  • 83
  • i am having a single page which contains pages rendered from different pages. And in each of its Onload i have given the script to delete the table but still its reflected in only one page.. And not in all the pages rendered to the main page.. – useranon Apr 23 '10 at 05:07
  • I can't quite understand what you mean. Can you give a url to the page in question, or an identical test case? – nikc.org Apr 23 '10 at 05:31