GithubHelp home page GithubHelp logo

creating-and-inserting-dom-nodes's Introduction

Creating and Inserting DOM Nodes

Objectives

By the end of this lesson, you'll be able to

  1. Create DOM elements programmatically
  2. Insert elements into the DOM
  3. Remove elements from the DOM

This lesson is a code-along - Learn will expect to see changes made to the file contents in order to register completion. Copy and paste the code snippets as you go to follow along.

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 or use httpserver to serve it temporarily. Once open, open up the browser's console. In the console, enter:

var element = document.createElement('div');

Type element. (or whatever you named your newly minted element), and explore the properties available. It's a living, breathing DOM element, but it doesn't yet appear in the DOM.

We can set properties on it:

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

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!

But notice that no matter what properties we add, the element doesn't show up on the page. What gives?

appendChild()

To get an element to appear in the DOM, we have to append it to an existing DOM node. 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. Let's append element to body to start:

document.body.appendChild(element);

If you've been following along, you should see "Hello, DOM!" on the page now (and it should have a light gray background).

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

element.style.textAlign = 'center';

And now our element's text is centered.

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

Hm, that looks a bit ugly. Let's fix it

ul.style.textAlign = 'left';

That's better.

removeChild()

Now let's remove one of those lis.

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! Run learn submit to submit the your code before continuing to the next lesson.

Resources

View Creating And Inserting Nodes on Learn.co and start learning to code for free.

creating-and-inserting-dom-nodes's People

Contributors

annjohn avatar gj avatar maxwellbenton avatar ngevan avatar pletcher avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

creating-and-inserting-dom-nodes's Issues

Setup as Readme and should be a Lab

The lesson tells students to:

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

However, there is no index.html file provided as the lab doesn't clone down. As far as I can tell.

Syntax Error

Entering the following code into the console returns a Syntax error (indicated below the code)

const divs = document.querySelectorAll('div')
function capture(e) {
console.log(this.firstChild.nodeValue.trim() + ' captured')
}
for (let i = 0; i < divs.length; i++) {
divs[i].addEventListener('click', capture, true)
}

Uncaught SyntaxError: Identifier 'divs' has already been declared
at :1:1

Finished light doesn't stay on

I'm not sure what makes it turn off, but I've clicked the Finished Reading button at least 4 times, and it doesn't stay on for more than a couple hours

There is no test

Perhaps the lab should not change... just my perception of it?

Alt Text

Nothing to submit

This lesson appears to be a code along. I cannot mark it as as complete as it is expecting a submission.

Ruby error when executing "learn test" command

Software: Learn IDE 3
O/S: Windows 10

Reproduction steps:

  1. Type "learn test" on the console;
  2. Press Enter;

Result:

/usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/lib/learn_test/strategies/mocha.rb:42:in `results': undefined method `[]' for nil:NilClass (NoMethodError)
        from /usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/lib/learn_test/reporter.rb:47:in `report'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/lib/learn_test/reporter.rb:13:in `report'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/lib/learn_test/runner.rb:44:in `report_and_clean'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/lib/learn_test/runner.rb:21:in `block in run'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/lib/learn_test/runner.rb:20:in `fork'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/lib/learn_test/runner.rb:20:in `run'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/learn-test-2.6.1/bin/learn-test:68:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/learn-test:23:in `load'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/learn-test:23:in `<main>'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'

No test cases to run

When I run $ learn open I get "It looks like this lesson is a Readme. Plese open it in your browser." But the lesson says I should run local tests/submit a pull request before proceeding.

What am I supposed to do?

What exactly am I supposed to do for this module? How do I access the tests within the IDE? And how do I submit? I'm typing the normal commands I've been using in index.js but nothing is happening.

Test

this is a Readme but runs as a test in learn to pass

Unable to complete.

Unable to complete this module as there are not test set up for students to complete.

This lesson should be a lab

@maxwellbenton
In order for a student to follow along with this lesson, they need to open the sandbox, manually clone the repo, and then run httpserver - it is a lot just for a codealong. I think it would be easier if we used the "Open IDE" button here instead.

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.