FAQ: Javascript and the DOM - Select and Modify Elements

This community-built FAQ covers the “Select and Modify 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 Select and Modify 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!

I don’t quite understand, we change an element in JS but in HTML it remain the same ?

html should be changed. Please post your html & js code, maybe that can help us identify the issue

document.querySelector('h1').innerHTML = "Most popular TV show searches in 2016"
document.getElementById('fourth').innerHTML = "Fourth element"`

Output

3. iPhone X
4. Fourth element
5. Meghan Markle

HTML

<li id="third">iPhone X</li>
 <li id="fourth">Matt Lauer</li>
1 Like

You expect the content of the html file to be changed? That won’t happen, the output changes

JS can manipulate the output (when pages is loaded), it won’t change the html file

why would you expect the html file to be updated?

Yeah, a bit confusing for me. I thought if output is changed HTML must be changed to.

1 Like

No, that is not the case.

A browser renders html (from top to bottom), which is why we put the <script></script> tags near the bottom.

So after the <body></body> is loaded, the script is read and the code executed. This will manipulate the rendered code.

1 Like

Why can’t we use document.h1.innerHTML='Most popular TV show searches in 2016'; instead of document.querySelector('h1').innerHTML='Most popular TV show searches in 2016'; ?

According to previous step, elements can be called and changed with innerHTML only and h1 is an element too. Am I missing something?

1 Like

yes, but document is an object containing certain methods like querySelector. h1 (and other elements) are not valid methods of the document object.

thankfully not, that would bloat document object massively, to include every element also as selector. Then is having methods to which you can pass arguments (querySelector method, 'h1' argument) is much cleaner and doesn’t violate DRY (don’t repeat yourself)

1 Like

By default, should you use ' ... ' or " ... " and why?

Remember that JavaScript modifies the DOM, not the HTML itself

These are both valid ways of writing strings. There isn’t actually a ‘preferred’ way, however, according to Google, '' should be used

Ordinary string literals are delimited with single quotes ( ' ), rather than double quotes ( " )

However, personally, I use double quotes, just because I prefer them. This will be the case for many JavaScript developers

1 Like

If querySelector() can access the elements such as tag, class or ID, why do we even need getElementByID() in the first place?

I tried both methods to access id #fourth and it works the same.

There are some good explanations on the matter, I would recommend reading them:

javascript - querySelector vs. getElementById - Stack Overflow

1 Like

Mate you are ■■■■■■ fantastic i am have been sat at my computer an hour wondering why the ■■■■ none of my code was working for this and OMG!!! I felt a right prat something so simple as I forgot to move my to the bottom of the ■■■■■■ page hahaha. Thank you

Edit ( I only used the work B,l,o,o,d,y, )