最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove table from <div> tag - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Apr 20, 2017 at 21:10 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 14, 2012 at 5:46 user1509user1509 1,1615 gold badges17 silver badges45 bronze badges 1
  • 3 Kindly go through the following link stackoverflow./questions/3387427/… – शेखर Commented Dec 14, 2012 at 5:50
Add a ment  | 

4 Answers 4

Reset to default 3

with relation to non jQuery:

Remove dom element without knowing its parent?

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

Get the element id and use remove()

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

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

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

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();
发布评论

评论列表(0)

  1. 暂无评论