JavaScript JavaScript and the DOM 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
PRO
Daniel Turato
Java Web Development Techdegree Graduate 30,086 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