Up till now we've seen that we can change the text inside the selected HTML element using this thing 1
called innerHTML, but there's actually another property that you can tap into, 2
and it's called the textContent. And it might seem at first glance that they do exactly the same thing, 3
they basically just change the text of whichever element that we select, 4
right? 5
But actually it's slightly different because the innerHTML does exactly what it says on the tin, 6
it actually gives you the HTML that's in between the element tags. 7
So for example if I tried to get the current value of the HTML, it's Hello. 8
But let's go into our code and if I add a strong tag around the word Hello, then I also get the strong 9
tags. But if I tried the same thing with textContent, then that's kind of exactly what it is, 10
right? 11
It just gives you the text content. 12
So what this means is that if you select the h1 element and then use innerHTML, then it will literally 13
give you all of the HTML that is inside that element tags, including any other HTML tags. 14
So this means that if you use innerHTML, you can also add HTML code on the fly. 15
So, for example, if we wanted our Hello to not only change its text to Good Bye, 16
but we also wanted that word to be emphasized, 17
so to be italicized effectively, then we can assign an em tag around the word, 18
and that goes into the innerHTML of the h1, and you can see that we changed the text and we also 19
applied that HTML element to the text. 20
But remember that when you're writing code like this, the HTML also has to go into quotation marks, because 21
the value has to be a string, because otherwise it's going to think it's Javascript code, and this is 22
certainly not Javascript code, right? 23
So remember that when you're changing this, be it innerHTML 24
or the style value, you always have to specify it as a string. 25
So have a play around with that and see if that makes sense, and apply some HTML using your Javascript. 26
And in the next lesson we're going to talk about the last thing that we can manipulate which is the 27
attributes of each element.