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

Controlling node insertions with insertBefore

From: JavaScript: Enhancing the DOM

Video: Controlling node insertions with insertBefore

AppendChild is a great method, but you don't always want to place an element as a child of another element. You need to be able to insert notes at any position inside a node list. If you watch the last movie, we sorted of cheated a little bit by already having a list item element, in which to place our image. That made it easier to use appendChild, but thankfully there's a great function called Insert Before that lets you place a node in a precise location. Let's take a look at that in a document, so this works a lot like a pen child.

Controlling node insertions with insertBefore

AppendChild is a great method, but you don't always want to place an element as a child of another element. You need to be able to insert notes at any position inside a node list. If you watch the last movie, we sorted of cheated a little bit by already having a list item element, in which to place our image. That made it easier to use appendChild, but thankfully there's a great function called Insert Before that lets you place a node in a precise location. Let's take a look at that in a document, so this works a lot like a pen child.

Before we use it I need to get into developer tools, so Cmd+Option+I, Ctrl+Alt+I on a PC, and then Esc. Now what I want to do is add a new paragraph in our venue, travel section. So let me click on the Venue/Travel section first and scroll down to right here. I want to put it right in between these two paragraphs. So I'm going to grab the magnifying glass. Click on one of these paragraphs, and there I am. I want to put that right before. So first we are going to create the paragraph element. I will create a variable called pNode and make that equal to a new element, so document.createElement and we want to create a paragraph element here. So I had written I have got my paragraph and now I need to create a text node for insertion into this paragraph so I will create a new variable call it myText. And this time I'm going to create a TextNode.

And now we need to append the text to our paragraph node. So this time I'll go ahead and use appendChild. So the pNode and appendChild, then I'll pass along my TextNode. So let's check out pNode right now. It should be a paragraph with the text that we specified. So in order to insert that in between the two paragraphs, we'll need to grab the location of the venue section. So I'll create another variable, this time I'll call it new node.

And I'll use query selector to find the element with an ID of the venue. Now that we have the node we need to analyze it a little bit. So we'll do a directory in this newNode and check out what's inside. So if we take a look, this article has seven child nodes. Now a lot of them are going to be text nodes that are just carriage returns. Here's the first paragraph, and here's the second paragraph. So what we want to do is insert a new paragraph before node five.

Which is the second paragraph, so what I'll do here is use this new node that I created and then say insert before. What I want to insert, which is pNode, the paragraph, and then insert it into new node and then childNodes and I'll want to place it before child node number five. So when I hit return, it should add that paragraph in between those other two paragraphs. So insert before is definitely a more surgical insertion than appendChild.

It's much easier to use when we want to insert things in specific place in the node list, it's the only way to go.

Show transcript

This video is part of

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

36 video lessons · 7793 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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.