JavaScript JavaScript and the DOM Making Changes to the DOM DOM Manipulation

Fabrice Innocent
Fabrice Innocent
7,212 Points

Next steps with setting the class attribute

A bit confused here. I'm assuming I should be building upon each steps code, so how do I assign a new attribute to the newParagraph variable if I have to already assign it to the "p" variable?

app.js
let p = document.createElement('p');
let att = document.createAttribute('panel');
att.value = 'panel';
var contentDiv = document.getElementById('content');
var newParagraph =  p;
newParagraph.setAttributeNode('panel')
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>DOM Manipulation</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <div id="content">

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

1 Answer

var contentDiv = document.getElementById('content');

var newParagraph = document.createElement("p"); /* Create it right away without making an individual variable for it */

newParagraph.className = "panel"; /* Add/change the class name to "panel" */

contentDiv.appendChild(newParagraph); /* Attach the new paragraph to the desired node. */

Hope this helps!

Fabrice Innocent
Fabrice Innocent
7,212 Points

Thanks so much! Will be going back over the videos to make sure I grasp the concept a bitt better