0

I am getting error some this like this.

Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.

my div is some what like this.

<div class="" style="" id ="P1">
 <div class= " " style="" id ="C1">
 <Input>Val</Input>
 </div>
 <div class= " child2" style="" id ="C2">
 <Input>Val2</Input>
 </div>
</div>

I want to remove all the child but getting the error.

var myClass = val.("Data").el.dom // My Parent div
 myClass.removeChild();

Here is what I am trying. Can anybody help me how to get this.

asked Nov 4, 2017 at 10:17
3
  • In dom element. Wanted to remove from dom. Commented Nov 4, 2017 at 10:20
  • Data is id of my component. Commented Nov 4, 2017 at 10:22
  • are you using jquery ? in which event you want to remove all child div ? Commented Nov 4, 2017 at 10:22

3 Answers 3

2

To remove all the child of div with id P1 you may simply do this :

document.querySelector("#P1").innerHTML = "";
//or use this 
//document.getElementById("P1").innerHTML = "";
//or a jQuery solution like this
// $('#P1').html("");
<div class="" style="" id="P1">
 <div class=" child1" style="" id="C1">
 <Input>Val</Input>
 </div>
 <div class=" child2" style="" id="C2">
 <Input>Val2</Input>
 </div>
</div>

answered Nov 4, 2017 at 10:21
Sign up to request clarification or add additional context in comments.

1 Comment

I am only able to access id not class
0

removeChild method removes a specific child node.If need to remove all the child ,put it in a while loop

firstChild returns the first nested element. On successful removal, the next child will be the first child,so as long as firstChild is there, it will execute

var prtElement = document.getElementById('P1');
while (prtElement.firstChild) {
 prtElement.removeChild(prtElement.firstChild);
}
<div class="Parent" style="" id="P1">
 <div class=" child1" style="" id="C1">
 <Input>Val</Input>
 </div>
 <div class=" child2" style="" id="C2">
 <Input>Val2</Input>
 </div>
</div>

answered Nov 4, 2017 at 10:23

Comments

-1

I would recommend using jQuery:

$(document).ready(function(){
/* or whatever else function you want */
$("#parent").children().remove();
});
answered Nov 4, 2017 at 10:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.