Viewers: in countries Watching now:
Making a copy of a node is called cloning and it can be useful if you want to move things around in your DOM. Let's say that you want to dynamically change the position of an existing element, the cloneNode method lets you make a copy of an existing node with our without its children. You can then reposition the node anywhere within the existing DOM. You can use removeChild to delete a note from the DOM, but you've gotta be careful with this one. RemoveChild works by removing a node from an existing parent. So if you want to use it, you've got to call it on the parent of the node you want to delete.
So, let's take a look at this in an actual DOM. So, we've got our website right here. I'm going to pull up the console and I'm going to create a variable called my node and make it contain a node that's going to have the artist lists which is right here. So, I'm going to hit the magnifying glass and click on one of these items. You can see the breadcrumbs pointing to that element and when we create the variable we'll use querySelector. And call the element with the class artist list. So, be careful here.
You want to make sure you use querySelector, not querySelector Alt, or you'll get an array of all the elements. So, let's check out what my node has. So, this has the entire list of artists right here. To clone an element you create a new node and use the clone node method, then you pass it along a true or false value. If you pass along a true value it means that all of the child nodes will also be copied as well. So it's perfect for copying something big, like our list of artists. So I am going to make a new variable here call it new node.
And I'm going to make it equal to my node, and then use the clone node method, and pass along the valley of true. So by passing true, I'm not just copying the node, I'm copying the node and all its children. So I hit Enter. Now let's check out this new node. We'll go ahead and issue a directory command on this new node. So we'll hit Enter and we can analyze this node. You can see that it has all the children from the previous node. So I want to insert it before the my schedule promo on the homepage so lets take a look at that. So we have got coming to that event here, and we have got this schedule area right here.
That's where we want to put it so I am going to hit the magnifying glass once again and just click on that area right there. And we know by the breadcrumbs that we want it right around this area. So let's go ahead and target this insert location. So we'll create another variable, call it insert location and I'm going to make it equal to document. And then I'll use query selector. And we want to ask for the entire sidebar element. So, we'll do a pound sidebar that targets this sidebar element right here, which contains all the different pieces. So, this top piece right here coming to the event, as well as this other scheduled piece.
So let's go ahead and investigate this new sidebar element we've got now. So do a directory on insertLocation and take a look at that. So if we open this up we can see that it has seven child nodes and the schedule child node happens to be element number four. So we want to insert it right before element number four. So let's check that out right here. There's the beginning of that element. We want it to be right here. So I'm going to scroll all the way down and go to our insertLocation and then use the insertBefore method and then pass it along the new node, as well as the place where we want to insert this.
We want it to be in the insertLocation and then look for child nodes. Element four. That looks pretty good, but I've got a couple of duplicate nodes. It would be great to remove the original nodes, and to do that we can use the method called remove child. So, you think there'd be a function that just removes an existing node. But remove child works by removing a node from a parent. So I need to use it not on my node, but on the parent of my node. So let's check that out. We'll do myNode, which is the original node and then parentNode and say removeChild myNode.
This is a little weird. Once you get your head around this, you can remove any node in the DOM. Now the node containing my original list of artists is gone. So hopefully you are getting comfortable managing your DOM. These two methods are really crucial to understanding how to move things and change things within your existing DOM.
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.