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

Chris Gutierrez
Chris Gutierrez
6,911 Points

I am appending the newParagraph to the body. why isn't it working?

Please help.

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

//Remove "Please Enable JavaScript" paragraph

var p = pleaseEnableParagraph.parentNode
p.removeChild(pleaseEnableParagraph);
//Append new paragaph to document
newParagraph.append(body);
index.html
<!DOCTYPE html>
<html>
  <body>
    <p id="please_enable">Please Enable JavaScript</p>

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

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Chris,

You don't need to set up a new variable for the parent as 'body' is already defined. You just need to removeChild from body.

Then you need to append the newPara to the body. Try it this way:

app.js
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);
Brian McCune
Brian McCune
2,966 Points

For some reason it registered as a comment not an answer...

2 problems that I can see..

1) you don't have anything added to the new paragraph. 2) append is not the correct method. Try appendChild(); Mozzila Documentation

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

//Remove "Please Enable JavaScript" paragraph

var p = pleaseEnableParagraph.parentNode
p.removeChild(pleaseEnableParagraph);
//Add text to the new paragraph
var newContent = document.createTextNode("Your new Paragraph"); 

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