Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  107] [ 7]  / answers: 1 / hits: 64430  / 13 Years ago, fri, may 6, 2011, 12:00:00

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




More From » javascript

 Answers
11

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


[#92374] Wednesday, May 4, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neob

Total Points: 253
Total Questions: 106
Total Answers: 104

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
;