Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Appending and Removing Elements

Tiffany White
Tiffany White
5,373 Points

Not sure what is wrong with my code here.

I am using the element body and the removeChild(); method on "p" which is the child I want to remove but body is wrong and I am not sure what to use there.

app.js
var body = document.body;
var newParagraph = document.createElement("p");
var pleaseEnableParagraph = document.querySelector("#please_enable");

//Remove "Please Enable JavaScript" paragraph
body.removeChild("p");


//Append new paragaph to document
index.html
<!DOCTYPE html>
<html>
  <body>
    <p id="please_enable">Please Enable JavaScript</p>

    <script src="app.js"></script>
  </body>
</html>

3 Answers

The issue is in the code to remove the paragraph, you've already setup a variable containing the actual paragraph you want to remove, you just need to pass that variable to the remove call thusly:

body.removeChild(pleaseEnableParagraph);

instead of

body.removeChild("p");

because removeChild needs to know the "specific" paragraph you want to remove.

Paul Ryan
Paul Ryan
4,584 Points

Hi,

You are tryiing to remove the wrong paragraph. You need to remove the please enable javascript paragraph. You can simply pass the pleaseEnableParagraph variable into the removeChild method and that will remove the paragraph.

I will let it up to you on how to append the newly created paragraph. :)

Hello Triffany This is the complete answer for all 2 steps of the challenge <blockquotes>

var body = document.body;
var newParagraph = document.createElement("p");
var pleaseEnableParagraph = document.querySelector("#please_enable");

//Remove "Please Enable JavaScript" paragraph
body.removeChild(pleaseEnableParagraph);


//Append new paragaph to document
body.appendChild(newParagraph);

</blockquotes> In the first step we removed the child 'pleaseEnableParagraph' from the parent body element by using 'removeChild' method. Next we appended the child 'newParagraph' to the parent body element by using the appendChild method. Hope my answer helped u..pleasse vote or mark my answer as the best