Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove div tag content?

Tags:

javascript

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()'>
like image 534
holian Avatar asked May 06 '11 08:05

holian


People also ask

How do I clear a div content?

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.

How do I hide everything in a div?

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.

How do I remove a div tag in Wordpress?

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!

How do I set div to not display?

display = "none" will make it disappear when clicked on div.


2 Answers

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

like image 52
DanielB Avatar answered Oct 05 '22 22:10

DanielB


You can use:

document.getElementById("level3").innerHTML = "";
like image 27
Billy Avatar answered Oct 06 '22 00:10

Billy