Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM Making Changes to the DOM DOM Manipulation

Jonathan Guerrero
.a{fill-rule:evenodd;}techdegree seal-36
Jonathan Guerrero
Front End Web Development Techdegree Graduate 18,137 Points

how do I assign a <p> into a variable?

I tried everything I could think of and still nothing works. Please help. Thank You

var contentDiv = document.getElementById('content');
var newParagraph = document.getElementsByTagName('p')[0];
<!DOCTYPE html>
        <title>DOM Manipulation</title>
    <link rel="stylesheet" href="style.css" />
        <div id="content">
        <script src="app.js"></script>

2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Jonathan Guerrero! The key to understanding this task is the first word ... "Create". The challenge means to create a paragraph without altering the HTML. You will need to erase the <p></p> that you've added to the HTML.

Also, contrary to the previous answer, the element does not have to be in all caps. You can check out the createElement for more information about how to create an element using only JS and not altering the original HTML file.

This would have also been fine assuming that you remove the <p></p> from the HTML.

var contentDiv = document.getElementById('content');
var newParagraph = document.createElement('p');

Hope this helps! :sparkles:

Hi Jonathan!

This passes (task 1):

var contentDiv = document.getElementById('content');
var newParagraph = document.createElement('P'); // The element name must be in all caps

I hope that helps.

Stay safe and happy coding!