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

Unable to assign paragraph element to the given variable

As shown in the code, I have to create a paragraph element and assign it to variable newParagraph. I tried using document.getElementById , getElementsByTagName('p')[0] and getElementsByClassName (I gave the tag some id and class). I was able to manipulate the element using .style and everything. However, I am unable to complete the task. When I click on 'Check Work', I get an error saying "Make sure that you assign a '<p>' element to 'newParagraph'." Please help me solve this problem.

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

1 Answer

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

I believe that you're not meant to create the paragraph in raw html but instead create it through javascript. You can achieve this by using this method. You can read more about this in the docs here - https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

document.createElement('p');

Thank you. I had just finished watching the video and still forgot to use createElement()..how silly of me, haha