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

Fabrice Innocent
seal-mask
.a{fill-rule:evenodd;}techdegree
Fabrice Innocent
Full Stack JavaScript Techdegree Student 9,715 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

Armin Kadic
Armin Kadic
16,242 Points

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!