Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove child div using javascript

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

like image 589
Bhargavi Avatar asked Dec 06 '22 05:12

Bhargavi


2 Answers

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);
like image 113
Sam Braslavskiy Avatar answered Dec 28 '22 21:12

Sam Braslavskiy


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
like image 40
Satpal Avatar answered Dec 28 '22 23:12

Satpal