user1509
user1509

Reputation: 1161

Remove table from <div> tag

I have a <div> tag in which I add <table> dynamically through Javascript:

var htmlText = "<table id='table_" + id + "'><tbody>";
htmlText += "<tr> <td><img src='../../Icons/action_delete.gif' onclick='javascript:RemoveUser(\"" + id + "\");' /></td> <td>" +  name + " </td></tr>";
htmlText += "</tbody></table>";
document.getElementById("divSearchUsers").innerHTML += htmlText;

I add multiple table to the div. Now I want to remove a particular table. I get the ID of the table in RemoveUser function. How do I proceed for it?

Upvotes: 0

Views: 6939

Answers (4)

Adil
Adil

Reputation: 148110

Since id of html element is supposed to be unique, you can directly delete it using remove methos.

With jQuery

$('#tableIdToRemove').remove();

With Javascript

tableIdToRemove = document.getElementById("tableIdToRemove");
tableIdToRemove.parentNode.removeChild(tableIdToRemove);

or

If you have html and there is chance of duplicate ID out side the parent table then you can access the table to delete in relation to its parent table as follow.

$("#divSearchUsers").find('#tableIdToRemove').remove();

Upvotes: 2

user1855153
user1855153

Reputation: 579

if you want to remove the inner html then you should just do something like that:

document.getElementById('table_' + id).innerHTML = "";

Upvotes: 2

Koenyn
Koenyn

Reputation: 704

with relation to non jQuery:

Remove dom element without knowing its parent?

function removeElement(el) {
    el.parentNode.removeChild(el);
}

Upvotes: 3

Doink
Doink

Reputation: 1162

Get the element id and use remove()

$("#table_id").remove();

Upvotes: 2

Related Questions