How can I remove the content of a div tag using JavaScript?
I have tried some code, but it only removes the level1
div (and of course all o it childern), but how can I remove only the content in level3
inside?
function destroyDiv() {
var div = document.getElementById("level1");
div.parentNode.removeChild(div);
}
<div id="level1">
<div id="level2">
<div id="level3">
<label> Test content </label>
</div>
</div </div>
<div id=blahDiv>Remove me</div>
<input type=button value='Remove the div' onclick='destroyDiv()'>
JavaScript provides the functionality of clearing the content of div. There are two methods to perform this function, one by using innerHTML property and other by using firstChild property and removeChild() method.
You can hide an element in CSS using the CSS properties display: none or visibility: hidden. display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.
All you need to do is place your cursor within the div-wrapped paragraph in the visual editor, and hit Ctrl + 1 (or Cmd + 1 on Macs) twice. This will first wrap the paragraph in <h1> tags, then remove those tags – and the <div> tags at the same time. Hey presto!
display = "none" will make it disappear when clicked on div.
This sould work document.getElementById("level3").innerHTML = '';
but try thinking about using jQuery, because .innerHTML
implementation differs in some browsers. jQuery would look like $('#level3').html('');
You can use:
document.getElementById("level3").innerHTML = "";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With