Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.


How to Create and Append Nodes in JavaScript

Creating and appending nodes provides you with in-depth training on Developer. Taught by Ray Villalo… Show More

JavaScript: Enhancing the DOM

with Ray Villalobos

Video: How to Create and Append Nodes in JavaScript

Creating and appending nodes provides you with in-depth training on Developer. Taught by Ray Villalobos as part of the JavaScript: Enhancing the DOM
Expand all | Collapse all
  1. 2m 36s
    1. Welcome
    2. What you should know before watching this course
    3. Using the exercise files
  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

please wait ...
Creating and appending nodes
Video duration: 4m 27s 2h 3m Intermediate


Creating and appending nodes provides you with in-depth training on Developer. Taught by Ray Villalobos as part of the JavaScript: Enhancing the DOM

Developer Web
HTML JavaScript

Creating and appending nodes

So far we've been looking at ways of selecting and modifying DOM elements. Next we'll be taking a look a creating DOM elements that have never existed before. Creating new nodes is super simple. The create element method lets you add an element to the current document. Once you create the element though, it exists in memory, but it's not apart of the current DOM. The append child method is used to add the element as a child of an existing node. So let's take a look at this on a page. On this homepage, I've taken one of the authors out of my list. So let's go ahead and add here back in.

First I'm going to turn on the developer tools, by hitting Cmd+Option+I, Ctrl+Option+I, on a PC, and then hitting Esc. And let's go ahead and move this up a little bit so we can see them all. And what I want to do is click on the magnifying glass and find the author right next to the missing author, right here. And if you look at the elements panel you could see that the, author that's missing. Has a list item, but there's no image tag inside the list item, like the other authors. So first we need to create an image element using the Create Element method.

Let's go ahead and make a variable here. We'll call it MyElement. And I'm going to set it to the document and call the Create Element method and pass it along the element that I want to create. So what I need here is an image tag, I hit Return. You can see that my element now has an empty image tag. So, obviously this element is going to require some attributes. We can insert those with the set attribute method or by simply using dot notation. So let's try that. We'll do my element. Obviously I'm going to need a location of the image file and that's going to be in the images folder under artists. And then the name of the person LaVonne LaRue, and now and then jpeg, plus we need to add an Alt tag.

So this time I'll do myElement.alt and that's going to be syntax which just says photo of LaVanne LaRue. Okay, so, let's take a look at my element now. So, it has the image tag plus the source and alt attribute. This particular artist has non standard HTML attribute of data role speaker. So, we're going to use the set attribute method for this one since I can't use dot notation to access a non standard attribute.

So, now I'm going to use set attribute, and I'll pass along data, task, and speaker. So, now if we take a look at my element and see that it has all the attributes that it needs. So you might be wondering why we're not seeing anything on the screen. And that's because a new DOM element can exist in memory but it's not really a part of the DOM until we put it in an existing location. So I need to insert this element inside the list item tag that doesn't have an image. So since we're going to need to find that I'm going to create a variable called myNode and look for that element. I'll go to document and use querySelectorAll and what I want to look for is the artist list.

The unordered list with a list item. So, I'm using the querySelectorAll and that's going to find all of the list items within the artist lists. If you look at your breadcrumbs, you can see that the artist list class Is right above our list items and it has the entire list so let's go ahead and hit enter here. And if we take a look at my node now so let's do a directory my node and we'll open this up. You can see that the one that we need, should be this list item, number 6.

So if I kind of, select it, you can see it right here, in the DOM. So I need the list item with an index of 6. So what I'm going to do is, access myNode and, specifically the sixth element, and then type in append child. And the name of the element we add which is that list that we created earlier. One I do that, it's going to add the image right where it needs to be right here. So, creating notes is really not a big deal but adding them to the dom means understanding a lot of what we've learned so far in previous movies.

Of course sometimes it's easier to use inner HTML. Just add some HTML, so, in most applications, your going to need to decide what option you want to to use.

There are currently no FAQs about JavaScript: Enhancing the DOM.






Don't show this message again
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

* Estimated file size

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


You have completed JavaScript: Enhancing the DOM.

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


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a Member and Create Custom Playlists

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

Get started

Already a member?

Log in

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:

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.

You started this assessment previously and didn’t complete it.

You can pick up where you left off, or start over.

Resume Start over

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 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 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

Sign up and receive emails about 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.