Skip to content
Advertisement

Want to remove previously appended table

When I Click on submit button after clicking on the links it appends perfectly but when I hit the button again it doesn’t remove previously appended table. I want to clear the previously created table when user clicks on the cross button and then print the table again or else overwrite the table but instead it is not removing the table and prints a new one.Image Part OneImage Part TwoImage Part ThreeImage Part Four

//variables
var order1 = document.getElementById('one').innerText;
var order2 = document.getElementById('two').innerText;
var order3 = document.getElementById('three').innerText;
var order4 = document.getElementById('four').innerText;
var temp = 0;
var orders_list = []; //Array



//Object Orientation To Create Order And Then Add It In Array
function orders(name) {
  this.name = name;
  if (orders_list[temp] == null) {
    orders_list.push(name);
  }
  temp++;
}

//Main Function Which Creates Orders
function order_maker(order_name) {
  var order = new orders("." + order_name);
}


//To Append Child Each Time Submit Buton Is Pressed And Check the Loop
function loop(argument) {
  var i = 0;
  while (i < orders_list.length) {
    var temporary = document.createElement("table");
    var orders_temp_list = orders_list[i];
    temporary.innerHTML = "<tr><td>" + orders_list[i] + "</td><td onclick='remove(" + i + ")'>&times;</td></tr>";
    document.body.appendChild(temporary);

    //This Block Is That I was Checking
    if (argument == "f") {
      temporary.innerHTML = " ";
    }
    if (argument == "t") {
      console.log("Done");
    }
    i++;
  }
}


//To Remove The Specific Element User Want To Delete
function remove(id) {
  orders_list.splice(id, id);
  loop("t");
}
a {
  margin: 20px;
  padding: 30px;
}

table {
  border: 3px solid #242424;
}

tr,
td {
  padding: 20px;
}
<!DOCTYPE html>

<head></head>

<body>
  <a href="#" id="one" onclick="order_maker(order1)">Cake1</a>
  <a href="#" id="two" onclick="order_maker(order2)">Cake2</a>
  <a href="#" id="three" onclick="order_maker(order3)">Cake3</a>
  <a href="#" id="four" onclick="order_maker(order4)">Cake4</a>
  <form>
    <input placeholder="name">
    <input placeholder="email">
    <input placeholder="order">
  </form>
  <p id="para"></p>
  <button onclick="loop('t')">Click</button>
</body>

Advertisement

Answer

  • Update your remove function as function remove(el) { el.closest('table').remove(); }.
  • Update parameter in html as "</td><td onclick='remove(this)'>&times;</td></tr>".
  • And add orders_list = []; in the end of loop function.

Try it below.

//variables
var order1 = document.getElementById('one').innerText;
var order2 = document.getElementById('two').innerText;
var order3 = document.getElementById('three').innerText;
var order4 = document.getElementById('four').innerText;
var temp = 0;
var orders_list = []; //Array



//Object Orientation To Create Order And Then Add It In Array
function orders(name) {
  this.name = name;
  if (orders_list[temp] == null) {
    orders_list.push(name);
  }
  temp++;
}

//Main Function Which Creates Orders
function order_maker(order_name) {
  var order = new orders("." + order_name);
}


//To Append Child Each Time Submit Buton Is Pressed And Check the Loop
function loop(argument) {
  var i = 0;
  while (i < orders_list.length) {
    var temporary = document.createElement("table");
    var orders_temp_list = orders_list[i];
    temporary.innerHTML = "<tr><td>" + orders_list[i] + "</td><td onclick='remove(this)'>&times;</td></tr>";
    document.body.appendChild(temporary);

    //This Block Is That I was Checking
    if (argument == "f") {
      temporary.innerHTML = " ";
    }
    if (argument == "t") {
      console.log("Done");
    }
    i++;
  }

  orders_list = [];
}


//To Remove The Specific Element User Want To Delete
function remove(el) {
  el.closest('table').remove();
}
a {
  margin: 20px;
  padding: 30px;
}

table {
  border: 3px solid #242424;
}

tr,
td {
  padding: 20px;
}
<!DOCTYPE html>

<head></head>

<body>
  <a href="#" id="one" onclick="order_maker(order1)">Cake1</a>
  <a href="#" id="two" onclick="order_maker(order2)">Cake2</a>
  <a href="#" id="three" onclick="order_maker(order3)">Cake3</a>
  <a href="#" id="four" onclick="order_maker(order4)">Cake4</a>
  <form>
    <input placeholder="name">
    <input placeholder="email">
    <input placeholder="order">
  </form>
  <p id="para"></p>
  <button onclick="loop('t')">Click</button>
</body>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement