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 trialAlexandra Edwards
4,686 PointsHow do I set the class of a variable in JavaScript?
I am stuck on task 2 of 3 in the section on DOM manipulating.
const contentDiv = document.getElementById("content");
let newParagraph = document.createElement('p').className = 'panel';
<!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
zainsra7
3,237 PointsHi,
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
Antonio De Rose
20,885 Pointsconst 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
Alexandra Edwards
4,686 PointsAlexandra Edwards
4,686 PointsThanks so much Zain! :)
Jason Myers
6,443 PointsJason Myers
6,443 Pointsthank you very much dude, this had me seriously stuck !!