$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>html - how to delete a created table using javascript - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - how to delete a created table using javascript - Stack Overflow

matteradmin13PV0评论

I am having a hard time to figure out the solution. I have created two buttons in HTML. One to add table and the other to remove table. The corresponding onclick functions for the buttons are written in the <script> tags. Now onclicking the Add Table button, the table would be created. And on clicking the Remove Table button the above created table should be deleted. I haven't figured out how to remove the created table. Please help me out in the solution to remove the created table. Below is the code for the following.

HTML CONTENT:

<!DOCTYPE html>
<html lang="en-US">
  <head>
  </head>
  <body>
    <button type="button" onclick="addTable()">Add Table</button>
    <button type="button" onclick="removeTable()">Remove Table</button>
  </body>
</html>

JAVASCRIPT CONTENT:

<script language="javascript" type="text/javascript">
 function addTable() {
   var x = document.createElement("table");
   x.id = "table1";
   document.appendChild(x);

   var y = document.createElement("tr");
   x.appendChild(y);

   var z = document.createElement("th");
   z.innerHTML = "FirstName";
   y.appendChild(z);

   var a = document.createElement("tr");
   x.appendChild("a");

   var b = document.createElement("td");
   b.innerHTML("John");
   a.appendChild(b);
 }

 function removeTable() {
   var removeTab = document.getElementById('table1');
   document.removeChild('removeTab');

   // I am not getting the exact syntax to remove the above created table
 }
</script>

I am having a hard time to figure out the solution. I have created two buttons in HTML. One to add table and the other to remove table. The corresponding onclick functions for the buttons are written in the <script> tags. Now onclicking the Add Table button, the table would be created. And on clicking the Remove Table button the above created table should be deleted. I haven't figured out how to remove the created table. Please help me out in the solution to remove the created table. Below is the code for the following.

HTML CONTENT:

<!DOCTYPE html>
<html lang="en-US">
  <head>
  </head>
  <body>
    <button type="button" onclick="addTable()">Add Table</button>
    <button type="button" onclick="removeTable()">Remove Table</button>
  </body>
</html>

JAVASCRIPT CONTENT:

<script language="javascript" type="text/javascript">
 function addTable() {
   var x = document.createElement("table");
   x.id = "table1";
   document.appendChild(x);

   var y = document.createElement("tr");
   x.appendChild(y);

   var z = document.createElement("th");
   z.innerHTML = "FirstName";
   y.appendChild(z);

   var a = document.createElement("tr");
   x.appendChild("a");

   var b = document.createElement("td");
   b.innerHTML("John");
   a.appendChild(b);
 }

 function removeTable() {
   var removeTab = document.getElementById('table1');
   document.removeChild('removeTab');

   // I am not getting the exact syntax to remove the above created table
 }
</script>
Share Improve this question edited Sep 25, 2015 at 8:37 Alexandr Lazarev 12.9k4 gold badges39 silver badges47 bronze badges asked Sep 25, 2015 at 7:58 John DurnJohn Durn 453 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can not directly delete a DOM object. You must delete it via it's parent.

var removeTab = document.getElementById('table1');

var parentEl = removeTab.parentElement;

parentEl.removeChild(removeTab);

Well, in javascript you can't remove directly an element from DOM. You have to go to its parent and remove it from there.

Something like:

var removeTab = document.getElementById('table1').parentElement.removeChild(removeTab);
Post a comment

comment list (0)

  1. No comments so far