Git Product home page Git Product logo

jsdom-mod-removing-altering-and-inserting-html-lab-teacher-onboarding's Introduction

Creating and Inserting DOM Nodes

Problem Statement

Here we are at our last component of learning JavaScript DOM programming.

  1. Ask the DOM to find or "select" an HTML element or elements in the rendered page
  2. Remove the selected element(s) or insert a new element (and / or)
  3. Adjust a property of the selected element(s)

We've come to understand the DOM and have powerful tools for selecting the right elements. We now want to understand creating new nodes, deleting nodes, and updating nodes' properties.

Objectives

  1. Create DOM elements programmatically
  2. Append elements in the DOM
  3. Change properties on DOM nodes
  4. Remove elements from the DOM

Create DOM Elements Programmatically

document.createElement()

Creating an element in JavaScript couldn't be easier. Simply call document.createElement(tagName), where tagName is the string representation of any valid HTML tag (e.g., 'p', 'div', 'span', etc.).

Open this lesson's index.html file in your browser and open up the browser's console. In the console, enter

var element = document.createElement('div')

The element doesn't show up on the page. What gives?

Append Elements into the DOM

To get an element to appear in the DOM, we have to append it to an existing DOM node. To go back to our tree metaphor, we have to glue our new leaf onto a branch that's already there. We can start as high up on the tree as document.body, or we can find a more specific element using any of the techniques we've learned for traversing the DOM.

appendChild()

Let's append element to body to start:

document.body.appendChild(element)

We can continue to update element, since we have a reference to it:

We can append elements to that element:

var ul = document.createElement('ul')

for (let i = 0; i < 3; i++) {
  let li = document.createElement('li')
  li.innerHTML = (i + 1).toString()
  ul.appendChild(li)
}

element.appendChild(ul)

Change Properties On DOM Nodes

We can change properties on DOM Nodes to change their appearance.

element.innerHTML = 'Hello, DOM!'
element.style.backgroundColor = '#ff0090';

Ka-bam! You've changed what's on the screen!

Feel free to set as many properties as you'd like — this is a good chance to look around and explore different properties of DOM elements!

Let's adjust the display:

element.style.textAlign = 'center';
ul.style.textAlign = 'left'

That's better!

Remove Elements from the DOM

Now let's remove one of those lis.

removeChild()

Let's really use the power of querySelector and method chaining. The removeChild() method requires us to find a parent and tell it to remove its already-found child:

ul.removeChild(ul.querySelector('li:nth-child(2)'))

Boom. Second element is gone.

What if we want to remove the whole unordered list (ul)?

element.remove()

We can just call remove() on the element itself:

ul.remove()

And it's gone!

Moving On

In this lab you must edit the included index.js file.

  • Remove the main element.
  • Add an H1 with id of victory programmatically, using JavaScript. Inside the tag you should put "(your name) is the champion!"

Inside index.js, you'll see JavaScript comments telling you where to put the code. Run the tests with learn. When they pass, use learn submit to move on! Congratulations.

Conclusion

We learned how to create, append and remove elements in the DOM with JavaScript. What's amazing about this is that you now understand "DOM programming with JavaScript."

You can:

  1. Ask the DOM to find or "select" an HTML element or elements in the rendered page
  2. Remove the selected element(s) or insert a new element (and / or)
  3. Adjust a property of the selected element(s)

To create even richer experiences we can build on this foundation by:

  1. Learning more about (cue the trumpets) The JavaScript Programming Language
  2. Learning to set events that trigger DOM manipulation behavior.

As a taste of what lies ahead, open a new tab and try pasting this into the DevTools Console and pressing Enter. You might have to scroll, but you should see the contents of myName on the page, cycling through colors!

let myName = "Byron the Poodle";
let colors = [
  "red", "orange", "yellow",
  "blue", "green", "indigo",
  "violet"
];
let updateFrequency = 500;

let body = document.getElementsByTagName("body")[0];

let currColor = colors[0];
let colorPost = 0;

let newHeader = document.createElement("h1");
newHeader.innerText = myName;
body.appendChild(newHeader);

setInterval( function() {

  if (colorPost > colors.length)  {
    colorPost = 0;
  }

  newHeader.style.color = colors[colorPost];
  colorPost++;
}, updateFrequency);

You should be able to grasp what's going on here and even be able to make some fun edits! Good luck on your continued exploration of JavaScript!

Resources

jsdom-mod-removing-altering-and-inserting-html-lab-teacher-onboarding's People

Contributors

sgharms avatar jenmyers avatar annjohn avatar pletcher avatar ngevan avatar gj avatar

Watchers

James Cloos avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.