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

Boonsuen Oh
Boonsuen Oh
18,788 Points

JS & DOM coding challenge

Anyone help me out? Stuck at Task 2.

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

I haven't took that course and I'm not sure if that's what you were supposed to learn, but for me it passed with:

newParagraph.className ="panel";

It also passes with:

newParagraph.classList.add("panel");

element.classList allow you to remove a class (element.classList.remove("foo")). To add a class (element.classList.add("bar")). Or to check if an element contains a class (element.classList.contains("aClass")).

What you were trying to do with "panel.className = newParagraph;" is to assing a className to panel, which is not defined as a variable anywhere, and then you tried to give it a className of newParagraph, which is a defined variable. You cannot assign a variable as a className (that I am aware), a className has to be a string.

Boonsuen Oh
Boonsuen Oh
18,788 Points

Thanks Sergi I've tried newParagraph.className = "panel"; It works!

2 Answers

Steven Parker
Steven Parker
231,248 Points

The instructions for task 2 are worded awkwardly.

And it looks like you tried to follow them a bit too literally, while confusing the class name with an element.

The instructions say "Set the class of panel to the newParagraph.", but imagine if they said one of these instead:

  • Give the class of "panel" to the newParagraph
  • Set the class of panel on the newParagraph

I'd bet you would have gotten it on the first try with either of these.

Boonsuen Oh
Boonsuen Oh
18,788 Points

Yeah that is confusing...

This. So much this. I had to read the sentence like 4 times and still wasn't sure if that's what they wanted.

Gonzalo Blasco
Gonzalo Blasco
11,927 Points

newParagraph.className = "panel";