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

ParentNode is a raw interface and no object of this type can be created; it is implemented by Element, Document, and Doc

Trying to understand MDN docs. Came across this. Is it saying that ParentNode is a built in object/method that we have access to but we can't make our own?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I don't believe so, no. But I'm not sure why exactly you'd want to either. However, you can access the ParentNode of an element and set it to a variable. I've made a small example for you to show how this might be done. The style I'm using is in the head section instead of making a completely new file.

<!DOCTYPE html>
<html>
    <head>
       <title>Test</title>
        <style>
            .myClass {
                background-color: tomato;
            }
        </style>
    </head>
    <body>
        <div id="listDiv">
           <div class ="content">
                <h1>This is my content div!</h1>
           </div>
            <div id="output">
                <ul id="myList">
                    <li>Item #1</li>
                    <li>Item #2</li>
                    <li>Item #3</li>
                </ul>
            </div>
        </div>
         <script src="testjs.js"></script> 
    </body>
</html>

And here's the JavaScript:

var myParentNode = document.getElementById("myList").parentNode;
console.log(myParentNode);
myParentNode.className ="myClass";

The console will log out the div with the id of "output" as it is the Parent node of the ul with the id "myList". I then take that variable name and assign the class to it which results in a "tomato" background color. Hope this helps! :sparkles:

Thank you Jennifer, that is a beautifully illustrated example.