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.

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:


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

A simple function that would increase the image by clicking and then decrease it back by clicking. But all this can be done in CSS… While nothing is clear why this is all :slight_smile: But it’s already interesting.

let picture = document.querySelector('.bearPic');

picture.onclick = function() {  
  let zoom = 1;  
  if(picture.height == 240){
    zoom = 1;    
    zoom = -1;    
  picture.height = picture.height + zoom * 50;

Hi all,

Can someone explain to me why the following doesn’t work?
I’m trying to modify the first list (under the heading ‘Species’). When I try to create another list item and append it to the parent node of the ‘li’ items (which is ‘ul’), it doesn’t appear. Thanks!

let firstListItems = document.getElementsByTagName('li');

firstListItems[0].style.color = 'red'; // works ok

firstListItems[1].innerHTML = 'Something else'; // works ok

let newListElement = document.createElement('li');

newListElement.innerHTML = 'Fifth item';

firstListItems.parentNode.appendChild(newListElement); // not working

firstListItem is an array of all li elements in the document.

firstListItems[0].parentNode.appendChild(newListElement); // works ok

// firstListItem is not an apropiate name. Maybe listItems…

Hi everyone just wanted to mention that “innerHTML” has been used quite a few times over the last few lessons but, MDN have the following message about it with regards to innerHTML and textContent:
Differences from innerHTML
Element.innerHTML returns HTML, as its name indicates. Sometimes people use innerHTML to retrieve or write text inside an element, but textContent has better performance because its value is not parsed as HTML.

Moreover, using textContent can prevent XSS attacks.

Just a heads up hope it helps.
Happy Days
Big Shuff

1 Like

interesting thing… thanks for sharing!