Viewers: in countries Watching now:
There's a number of properties that allow you to change the content related to a node as HTML. The most common and oldest is innerHTML. It also has the most support in browsers. In newer browsers, you can also use outerHTML, which will include any tags within the current node. There's also a new function that lets you insert some additional HTML within a specified node. It's called InsertAdjacentHTML and it takes two parameters. First, a string that determines the insertion point and then the HTML. So, let's take a look at these.
I want to switch over to my document, and pull up the developer tools and the console. So, I'll start by creating a variable called mynode, and then targeting a node in the DOM. Now this time, I'm going to target the text node right here that has this article. So, I'm going to hit this magnifying glass and click right here on this headline. That tells me the path to that node, which I know is going to be an article with the ID about the event. So, let me check out that node, and sure enough, I got the entire article. Notice that it has article tags.
So, if we open this up, you can see there's an H2 right here and a paragraph. So, we can get the contents of the node with the innerHTML property. So, we say mynode.innerHTML. It's going to give us everything inside that node. Notice that it's not including the article tags, just whatever is inside the article tags. It's a read and write value, so we can set it to anything we want. And you can see now, I've replaced this article with this content right here.
There's a newer property introduced by HTML 5 called outerHTML. And in browsers that support it, it also includes the tags of the element that was called. So, let me go ahead and refresh this page. And I'm going to click on the console and hit the up arrow a few times to get to my variable. And then I'll hit enter, and I know have that node again. And if I say, myNode.outerHTML, notice that includes the article tag. There's also a new function that lets you insert some additional HTML within a specified note. It's called InsertAdjacentHTML, and it takes two parameters: a string that lets it know the insertion point and then the actual HTML we want to insert. So, let me try that one.
So, we need a string right here and then something we want to insert. I'll put in a paragraph right here. The string that I put in here will be one of four values. And that will determine the position of the element within the DOM. So you can say, before begin, after begin, before end, and after end. Let's try the after end property here. You notice that it added this paragraph right here. So, if I say something like, before begin, it'll add it at the top.
So, let's try doing another one. After begin, and this time I'm going to add a word right here. Notice that it added this after the first element. And in the same way we can do before end. This time it was added before the last element. So, although these are pretty cool, it does require newer browsers. So, most of the time people stick with innerHTML.
It's by far the easiest, safest and most common way to access content within a node.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.