Ali Cope

Lesson 6 - DOM Manipulation using createElement, appendChild, insertBefore, removeChild

We can use DOM to change the content in our HTML. We can add, change or remove a child element. We can even insert one element infront of another. We will do all of this starting with a basic list of favorite movies.

  1. Princess Bride
  2. Moana
  3. World War Z

createElement


We can use DOM to create a new element on our page. Click the button to add a new paragraph


code showing creation of new paragraph

appendChild


Okay back to our list. Instead of creating a new element we want to just change the list element we already created. We can do this with appendChild. The "li" is a child of our "ol" Click the button to add Joe vs the Volcano to our list of favorite movies
***Note - we actually appended the child in the first example to add text to the new paragraph we created***

  1. Princess Bride
  2. Moana
  3. World War Z

code showing how to change list

insertBefore


Tonight I watched Star Wars and I loved it. Now I want to add it as my number one movie. I can do this using insertBefore.

  1. Princess Bride
  2. Moana
  3. World War Z
  4. Joe vs. the Volcano

code to add new item to top of list

removeChild


I have now seen Star Wars several times and I am getting a little sick of it. As much as I love this movie I just have to take it off my list. This is easy to do with removeChild.

  1. Star Wars
  2. Princess Bride
  3. Moana
  4. World War Z
  5. Joe vs. the Volcano

code to remove image