5

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

1

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Do you have any idea regarding not to remove rather detach a concerned div.?
3

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

3 Comments

What's the difference between your answer and what I have posted earlier?
@SamBraslavskiy, Its not different but I was working on demo while you posted and I assure you its not copied
Totally believe you. Liked your answer, since we're at some degree like-minded)
2

Use modern JS!

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

or just

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

Comments

0

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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.