JavaScript JavaScript and the DOM 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

anthony amaro
anthony amaro
8,222 Points

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,408 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");

anthony amaro
anthony amaro
8,222 Points

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";