FAQ: Javascript and the DOM - Review

This community-built FAQ covers the “Review” exercise from the lesson “Javascript and the DOM”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The DOM tree is: document -> html -> body/head per the image at the start of this section.

We have document.body and document.head, but is there a way to reference document.html, or is that functionally equivalent to just document? Curious since if html is part of the DOM tree, there doesn’t seem to be a way to access it.

What is linking the two files i.e. index.html and main.js together?
In the exercise, we learn that document keyword gives access to the root node and from there on one can access various elements. I have a very basic doubt:
In the index.HTML file we link the script file using the tag. But nowhere we tell the main.js which document to access when we reference document. What tells the javascript engine which HTML file to look for?
An addendum to that question:

  • If there were two HTML documents say index.HTML and appendix.HTML and both included a script tag referring to a single js file say utils.js. This file may have some utility functions to add interactivity or some other frequently used functions. How would the utils.js figure out which file to reference using Document keyword.
    Thanks,

The html element isn’t the root element - in fact, it’s a child of the root element. To access the html element, you can use the .querySelector() method, like the following:

document.querySelector("html");

The script will run in the HTML document it is loaded in. So if we loaded it in index.html, it will be executed in index.html meaning the document object refers to the root node of index.html. If we loaded this script in two HTML documents, it will be equal to both! This might seem confusing, but whatever page you are on, the document will be equal to the root node of that document

1 Like