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

html - how to remove child div using javascript - Stack Overflow

programmeradmin1浏览0评论

this my html

<div>
    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>

this my script

var list=document.getElementById("div2");
list.removeChild("div2""));

when i click the buttton i need to remove the child div(div2) how to do that. using this code i am facing problem please tell. help me . we have any other solution for this problem

this my html

<div>
    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>

this my script

var list=document.getElementById("div2");
list.removeChild("div2""));

when i click the buttton i need to remove the child div(div2) how to do that. using this code i am facing problem please tell. help me . we have any other solution for this problem

Share Improve this question edited Oct 9, 2013 at 8:31 Raidri 17.6k11 gold badges66 silver badges68 bronze badges asked Oct 9, 2013 at 8:28 BhargaviBhargavi 8515 gold badges11 silver badges22 bronze badges 1
  • 1 Asked so many times stackoverflow.com/questions/3387427/… You should search before you ask. – TigOldBitties Commented Oct 9, 2013 at 8:35
Add a comment  | 

4 Answers 4

Reset to default 11

you have double quotes and double braces at the end. And I'm not sure what you're trying to do. If you'd like to remove element with the id "div2", use:

var list=document.getElementById("div2");
list.parentNode.removeChild(list);

You need to find div2 parent and then you can use removeChild to remve div2

var list=document.getElementById("div2");
var parentDiv = list.parentNode;
parentDiv.removeChild(list);

Demo

Problem in your code

list.removeChild("div2"")); <<== ") is additional

Use modern JS!

const div = document.getElementById("div2");
div.remove();

or just

document.getElementById("div2").remove()

try removing the elements like this

//HTML MARKUP

<div id="div1">
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>

//Javascript
var list=document.getElementById('div1');

list.removeChild(list.getElementById('div2'));
发布评论

评论列表(0)

  1. 暂无评论