1

I have a div element that contains a number of images. When the user clicks a button, I want the contents of the div to be removed (not the div itself, I want to reuse the div to potentially add new content).

In my HTML, the div and button are defined as:

<body>
    ...
    <div class="MyDiv"></div>
    ...
    <button id="removeDiv" onclick="removeDivFunction()">remove Div Function</button>
    ...
</body>

How do I write a function that removes all elements from this div?

David van Driessche
  • 6,602
  • 2
  • 28
  • 41
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • jQuery solution or no? – scniro May 15 '15 at 13:06
  • 1
    possible duplicate of [Remove element by id](http://stackoverflow.com/questions/3387427/remove-element-by-id) – David May 15 '15 at 13:07
  • 1
    http://red-team-design.com/removing-an-element-with-plain-javascript-remove-method/ – Nagaraj S May 15 '15 at 13:12
  • `div.innerHTML = ""` is that what you want? – Edwin Reynoso May 15 '15 at 15:14
  • To the author: I edited both title and content of your question so that it becomes more immediately apparent what you are looking for. Remember that the more clearly you state your question, the more chance you have that someone will go through the trouble of answering it. Also, if your question was answered correctly, *please* remember to 'approve' the correct answer. This rewards the author of that answer and makes it easier for future visitors to see what the correct answer was. – David van Driessche May 17 '15 at 15:56

6 Answers6

0

You have to call removeChild:

function removeDivFunction() {
   MyDiv.parentNode.removeChild(MyDiv);
}
Jivings
  • 22,834
  • 6
  • 60
  • 101
0
<div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">

This is some text in the div.
<p>This is a paragraph in the div.</p>
<p>This is another paragraph in the div.</p>

</div>
<br>

<button>Remove div element</button>


$(document).ready(function(){
    $("button").click(function(){
        $("#div1").remove();
    });
});
Aman Khanna
  • 137
  • 1
  • 10
0
function removeDivFunction() {
    $(".MyDiv").remove();
}
user3419778
  • 856
  • 3
  • 8
  • 11
0

David has already pointed you to an existing question/solution.
For reference consider reading: http://www.w3schools.com/js/js_htmldom_nodes.asp
Its always a good idea to assign id to the div.

Also if you are using jQuery you can delete element by $("#divid").remove() for reference see: https://api.jquery.com/remove/

I hope this helps.

0
<div class="MyDiv">I need to remove</div>
<button id="removeDiv" onclick="removeDivFunction()">remove Div Function</button>  
function removeDivFunction() {
    var element = document.getElementsByClassName("MyDiv")[0];
    element.parentNode.removeChild(element);
}  

DEMO

ozil
  • 6,930
  • 9
  • 33
  • 56
0

You can try Html

<div id="some">Example</div>
<button onclick="myFunction()">Click me</button>

javascript

function myFunction() {
    var child = document.getElementById("some");
    child.parentNode.removeChild(child);
}

And if you want to erase content of DIV then use

  child.innerHTML = "";
Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28