FAQ: Javascript and the DOM - Create and Insert Elements

#1

This community-built FAQ covers the " Create and Insert Elements" 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 _ Create and Insert Elements_

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!

#3

Okay, i cannot see why we have to assign a new id for our list element in this exercise,

Assign the new element an id of "oaxaca" by using the .id property on the new variable.

since we are adding another list child at the end of the existing id element ‘more-destinations’ with:

document.getElementById('more-destinations').appendChild(list);
#4

Because assigning an Id will make it easier to manipulate in the javascript. You will need to call for this Id later on in the assignments ;).

1 Like
#5

Aha. Is it considered a common practice to do that, whether we may or may not need to call the id in the future?

#6

I am not sure if it is, but I would. It improves readability, and it will make it more convenient in the event that you do would like to call it in the future.

1 Like
#7

Hi,

The question (1) asks to assign an “id” to the new element created, but the “Solution” provided (below) after question (2) does not feature an “id” . I thought the new element was to be added to an existing list?

=======================================================
let newDestination = document.createElement(“li”);
newDestination.innerHTML = “Oaxaca, Mexico”;
document.getElementById(“more-destinations”).appendChild(newDestination);

#8

Is there an OL or UL element in the HTML with id="more-destinations”? That’s what this line is querying. If found, the LI child node is appended to it.