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

Alexandra Edwards
Alexandra Edwards
4,686 Points

How do I set the class of a variable in JavaScript?

I am stuck on task 2 of 3 in the section on DOM manipulating.

app.js
const contentDiv = document.getElementById("content");
let newParagraph = document.createElement('p').className = '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>

2 Answers

Hi,

Here's the solution :

const contentDiv = document.getElementById("content");
let newParagraph = document.createElement('p');
newParagraph.className = 'panel';

You just need to set the className in the next line, your approach is also good but Treehouse doesn't evaluate performing this challenge task the way you did.

and for the next Task it asks you to append the newParagraph to contentDiv, so simply use appendChild() method on contentDiv.

const contentDiv = document.getElementById("content");
let newParagraph = document.createElement('p');
newParagraph.className = 'panel';
contentDiv.appendChild(newParagraph);

I hope it answers your question, Happy Coding :)

-Zain

thank you very much dude, this had me seriously stuck !!

Antonio De Rose
Antonio De Rose
20,885 Points
const contentDiv = document.getElementById("content");
let newParagraph = document.createElement('p').className = 'panel'; // so close, className is not a function
//hence you cannot do that way in JavaScript
//simply retreive the className attribute from newParagraph, and assign it to panel,
//just as you assign to a variable