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 JavaScript and the DOM (Retiring) Making Changes to the DOM DOM Manipulation

Assign element to variable

Create a paragraph element and assign it to the newParagraph variable.

Ive tried everything mentioned in the last two sections but Im not sure what I am doing wrong. Please help

app.js
var contentDiv = document.getElementById('content');
var newParagraph = document.getElementById('p');
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>DOM Manipulation</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <div id="content">
          <p>This is a paragraph</p>    
        </div>
        <script src="app.js"></script>
    </body>
</html>

2 Answers

the question says Create a paragraph element and assign it to the newParagraph variable. you are trying to get an element with an id of p. the way to create an element

const pa = document.createElement('p');

hope this helps

Gareth Partridge
Gareth Partridge
13,421 Points

The Following worked for me:

var newParagraph = document.createElement("p");

But what I dont understand is where is the p coming from ? there is no reference to the p anywhere. why is it not.

var newParagraph = document.createElement("newParagraph");

since you are creating an html element

<p></p> is a paragraph tag

you can create any element you need then you can assign a value to it

var newParagraph = document.createElement('p');
newParagraph.textContent  = "this is the value of the new paragraph";