FAQ: Javascript and the DOM - Tweak an Element

This community-built FAQ covers the “Tweak an Element” 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 Tweak an Element

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’m having a problem getting the text to display properly. I have:

 document.body.innerHTML = 'This is the text of the body element';

in main.js but when I run it the ‘y’ doesn’t appear in the word ‘body’ and the word ‘text’ displays extra characters.

I tried refreshing Chrome and also tried double quotes. I must be missing something in the JavaScript?

I heard that it is not good to use innerHTML for security reasons? can anyone explain why this is bad?

1 Like

Scroll down to ‘Security Considerations’…

Element.innerHTML MDN

4 Likes

Late here, but hopping on here since I just passed this test and had a question. Welcome answers and or resources to help understand this.

Q: Why use JavaScript and DOM to change the heading (or any other part of the DOM for that matter), and why not just go back to your HTML file and change it there? Or is this just to show us the interactivity or the functionality of what JavaScript can do? The exercise in question is: https://www.codecademy.com/paths/web-development/tracks/build-interactive-websites/modules/web-dev-interactive-websites/lessons/javascript-dom/exercises/tweak-element

If the answer is something that I will come across in a future lesson, let me know and I will keep it in mind :). Thanks!

The reason should be obvious. How would we deliver a unique experience to every user?

1 Like

Ahhh. Didn’t think of it that way, but now that you mention it, it makes sense. Thanks! :slight_smile:

1 Like
document.body.innerHTML = 'This is the text of the body element'

why can’t i use document.html.body ?

1 Like

According to the list of properties, document does not have html as a property. I confirmed that document.html returns undefined and document.html.body raises TypeError.

I am trying to understand the different ways this approach to changing the DOM can be used.

document.body.innerHTML = '<h1>This is now the heading of the body element</h1>'

So the above code worked to change the whole body as per the exercise.

But then I tried:

document.h1.innerHTML = '<h1>Just the heading</h1>'

and

document.body.h1.innerHTML ='<h1>Just the heading</h1>'

and neither of these had any effect on the DOM, it seems, as nothing changed in the way the website displayed.

I know that there are other methods (explained later in the lesson), but I just want to understand how this approach works. Is it that you can only access direct children of document in this way? So only head and body?

document.body gives you the body element. (The .body is a special case, which does not work for most elements.)
But
document.h1 does not give you the first h1 element (since .h1 in not a property nor method of the document object).
and
document.body.h1 does not give you the first h1 element in the body (since .h1 in not a property nor method of the body object).

In the lessons coming up, you’ll see that you’ll be able to do

const h1 = document.getElementsByTagName('h1')[0]; // first h1 element

or

const h1 = document.querySelector('h1'); // css selector

to assign the first h1 element object to a variable h1

And if you’d like to what’s written inside the h1 element, you might then do

h1.innerHTML = 'Just the heading';

or

h1.outerHTML = '<h1>Just the heading</h1>';
1 Like

This is referred to as a global object which comes with its own global identifier.

Not really replying to this as using the example.

As much as the language supports a range of global objects it is definitely something to steer away from in all pursuits. Sure in simple demonstrations, but not in production code. We really need to draw the line away from the global namespace, unless it actually proves convenient and effective. That’s a five per-cent gray zone left there for experimenters.

If you want a variable called ‘body’, then declare it and break away from the global object.

const body = document.querySelector('body')

We’ve effectively choked off the global variable by shadowing it. Good move, imho.

HTML that is taught by, ‘here is how to do this’ paradigm will be boring. When it is taught by, ‘here is what you can do’ paradigm, the sky is the limit. We actually DO control the DOM, and that even includes the markup. We can create our own HTML elements and style them. You won’t see this talked about much because it has no real benefit to the standards that have evolved. It is still entirely possible to take things in a totally new direction. That’s how elastic HTML really is.

1 Like

Thank you for your reply! Just out of interest, what is the difference between:

document.querySelector('h1').innerHTML ='Just the heading';

and

document.getElementsByTagName('h1')[0].innerHTML = 'Another heading'

Apart from the obvious that querySelector only selects the first case of h1, and the array index of getElementsByTagName can target any. But I’m wondering why ‘selector’ in one and ‘tag’ in another. You also mention ‘css selector’, which makes me thing Tag means html tag (which happens to be identical with the css selector in this case).

Apart from targeting one vs targeting any, is there another difference which explains the different names of ‘selector’ and ‘tag’ here?

document.querySelector can be used for more than just selecting the first element that has that tag name:

document.querySelector('h1') selects the first h1 element
document.querySelector('.green') selects the first element that has class green
document.querySelector('#example') selects the first element that has id example
More complicated css selectors also work.

If you wanted to something similar to an array of elements that matches the selector rather than just the first one,
you may use something like
document.querySelectorAll('.green')
to get an “array” of all the elements that have class green

1 Like

Appropriate that you used ‘finger quotes’ since it is not an array, per se, but an array-like structure called a collection. We can however access each element by index or scan the collection with .forEach if we first call on Array.from(collection) to parse out a shallow array.

If any of this is incorrect, then please do intervene with the correction.

1 Like