2

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. 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> 

4 Answers 4

4

Here's a rough sample

<html>
<head>
<script type="text/javascript">

    function removeTable(id)
    {
        var tbl = document.getElementById(id);
        if(tbl) tbl.parentNode.removeChild(tbl);
    }

</script>
</head>
<body>

<table id="toc" class="toc" border="1" summary="Contents">
    <tr><td>This table is going</td></tr>
</table>

<input type="button" onclick="removeTable('toc');" value="Remove!" />

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

4

Really want to delete the table altogether?

var elem = documenet.getElementById('toc');

if (typeof elem != 'undefined')
{
  elem.parentNode.removeChild(elem);
}

You could also hide the table rather than deleting it.

var elem = documenet.getElementById('toc');
elem.style.display = 'none';

If you need it later, you could simply do:

var elem = documenet.getElementById('toc');
elem.style.display = 'block';

4 Comments

I think he's better off hiding it via a style
@Pierreten: Yes, it was important to post that. Thanks
Only in somecases the table gets removed. but not all the times .. Y so ??
This solution works for me but I wonder no one mentioned spelling mistake for documenet instead of correct one document.
0
<script type="text/javascript">
  function deleteTable(){
    document.getElementById('div_table').innerHTML="TABLE DELETED"
  }
</script>

<div id="div_table">

 <table id="toc" class="toc" border="1" summary="Contents">
 </table>
</div>
<input type="button" onClick="deleteTable()">

Comments

-1
var tbl = document.getElementById(id);
tbl.remove();

1 Comment

remove() is not a function it says:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.