Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Cloning and removing nodes

From: JavaScript: Enhancing the DOM

Video: Cloning and removing nodes

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.

Cloning and removing nodes

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.

Show transcript

This video is part of

Image for JavaScript: Enhancing the DOM
JavaScript: Enhancing the DOM

36 video lessons · 8829 viewers

Ray Villalobos
Author

 
Expand all | Collapse all
  1. 2m 36s
    1. Welcome
      59s
    2. What you should know before watching this course
      50s
    3. Using the exercise files
      47s
  2. 24m 33s
    1. What is the Document Object Model (DOM)?
      3m 2s
    2. Navigating the DOM with developer tools
      8m 10s
    3. Testing JavaScript commands with the console
      5m 50s
    4. Communicating with the console through JavaScript
      7m 31s
  3. 31m 9s
    1. Selecting elements with getElementById
      4m 10s
    2. Choosing elements by HTML tag
      3m 20s
    3. Isolating elements by class name
      3m 12s
    4. Querying CSS to select elements
      4m 54s
    5. Working with named form elements
      3m 39s
    6. Understanding nodeType, nodeName, and nodeValue
      4m 30s
    7. Traversing up and down DOM nodes
      4m 40s
    8. Targeting node elements
      2m 44s
  4. 22m 25s
    1. Changing HTML attributes
      5m 25s
    2. Working with restricted attributes
      2m 49s
    3. Detecting data attributes
      3m 29s
    4. Controlling classes with the HTML5 classList
      3m 21s
    5. Targeting the attributes property
      1m 24s
    6. Using text content modifiers
      3m 42s
    7. Modifying elements as text
      2m 15s
  5. 14m 57s
    1. Creating and appending nodes
      4m 27s
    2. Controlling node insertions with insertBefore
      3m 17s
    3. Cloning and removing nodes
      4m 41s
    4. Replacing existing nodes
      2m 32s
  6. 26m 14s
    1. What we'll build
      2m 16s
    2. Adding a bubbling event listener
      4m 11s
    3. Creating and styling an overlay with JavaScript
      4m 39s
    4. Adding an image
      3m 48s
    5. Resizing images in the DOM
      2m 59s
    6. Centering an image
      2m 36s
    7. Handling clicks
      1m 29s
    8. Adjusting for scrolling
      1m 36s
    9. Detecting and adjusting for a window resize
      2m 40s
  7. 1m 49s
    1. Next steps
      1m 49s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript: Enhancing the DOM.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.