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 trialAlexandru Pescaru
6,410 Pointswho do i create a new class?
i'm a bit lost
var contentDiv = document.getElementById('content');
var newParagraph = document.createElement('p');
<!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
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi there Alexandru,
This is about manipulating the DOM so you add a new class attribute that wasn't there before.
So, when you're working with the DOM you need to look at the HTML document tree, work out which element to select (that is to say what element to do something with and then perform an action on it.
In this challenge, we're selecting an element with the id of contact and you want to add a new paragraph element.
- First create the paragraph element using DOM scripting with the method that makes a
create
s an element. - Secondly, in JavaScript you can use a property to set the class of an element, so the second task is about that.
- Finally, creating the element doesn't add it to the DOM. To achieve this, you need to add the element as a child of the parent element.
Alexandru Pescaru
6,410 Pointsbut how to create in javaScript and set the class attribute
Jonathan Grieve
Treehouse Moderator 91,253 PointsYou need to use the className property. You can't use class
as that is a reserved keyword in JavaScript. :-)
To create an element, that is to make it available to use in the DOM try the createElement()
method and pass it the string of the element name you want to create which in this case is "p". Good luck! :-)