Return to Video

Creating elements from scratch (Video Version)

  • 0:01 - 0:07
    Okay, so far we've been modifying
    a lot of existing elements on the page.
  • 0:07 - 0:10
    But what if we want to add
    new elements to the page?
  • 0:10 - 0:15
    We could do that with `innerHTML`,
    writing the HTML for the tags
  • 0:15 - 0:19
    inside the string that we
    pass in, like we did here.
  • 0:19 - 0:22
    That can get a bit messy though,
    especially if you want to create
  • 0:22 - 0:26
    multiple tags with different
    attributes, styles, and classes.
  • 0:26 - 0:31
    So instead, we can use a whole suite
    of sweet document methods
  • 0:31 - 0:35
    for creating new elements from scratch
    and adding them to the page.
  • 0:35 - 0:41
    Let's say that we want to add
    an image of a cat to the page,
  • 0:41 - 0:44
    because we just don't
    think it has enough yet.
  • 0:44 - 0:49
    The first step is to create
    that new `` element, right?
  • 0:49 - 0:51
    We want to create that.
  • 0:51 - 0:56
    So we'll start off by creating
    a variable to store it, `catEl`.
  • 0:56 - 1:01
    And then we're going to use
    `document.createElement`,
  • 1:01 - 1:07
    and pass in the name of the tag
    we're making, `img`.
  • 1:07 - 1:12
    So now you can imagine that the browser
    has made an image tag, like this,
  • 1:12 - 1:15
    and it's floating off in space somewhere.
  • 1:15 - 1:19
    The next step is to assign a source to it.
  • 1:19 - 1:23
    So, `catEl.src =`,
  • 1:23 - 1:29
    and let's just grab our source
    from up here,
  • 1:29 - 1:35
    and-- oh, we should add an `alt`,
    to make this image more accessible--
  • 1:35 - 1:42
    haven't been doing that, and really should
    always have `alt` tags` on our images.
  • 1:42 - 1:47
    So now you can imagine that this
    `` tag that we've made
  • 1:47 - 1:58
    has a `src`, and it also has an `alt`,
    "Photo of cute cat".
  • 1:58 - 2:05
    Okay, so this is what we've made
    using this JavaScript here.
  • 2:05 - 2:09
    The `` tag that we made
    is still floating off in space,
  • 2:09 - 2:12
    because we haven't
    told the browser where to put it.
  • 2:12 - 2:16
    And there's so many different places
    in our DOM where it could go.
  • 2:16 - 2:21
    Let's do the easiest thing, and just
    make it show up at the bottom of the page.
  • 2:21 - 2:26
    We can do that by sticking it at the end
    of the `` tag, so we say:
  • 2:26 - 2:30
    `document.body.appendChild(catEl);`
  • 2:30 - 2:31
    Haha-- there it is!
  • 2:31 - 2:33
    It's quite large as well.
  • 2:33 - 2:35
    Very large cat-- scary.
  • 2:35 - 2:41
    Now you can call `appendChild` on
    any existing DOM node in your page,
  • 2:41 - 2:47
    and it will make the passed in element
    the final child of that node.
  • 2:47 - 2:52
    This is where it helps to really
    visualize the DOM as a tree.
  • 2:52 - 2:58
    The `` tag is a node in that tree,
    and it has a bunch of children,
  • 2:58 - 3:00
    like `` and ``,
  • 3:00 - 3:04
    and you're adding one more child
    at the end of its children.
  • 3:04 - 3:09
    So, actually it'd be after the
    `` tag, right here.
  • 3:09 - 3:13
    Using DOM methods, you should
    theoretically be able to append elements
  • 3:13 - 3:16
    anywhere inside the DOM tree.
  • 3:16 - 3:19
    We just put it in the easiest,
    most obvious spot.
  • 3:19 - 3:22
    Okay, let's do one more example.
  • 3:22 - 3:28
    We used `innerHTML` down here, to put
    `` tags inside the ``s.
  • 3:28 - 3:31
    Instead, we could use `createElement`.
  • 3:31 - 3:33
    [typing]
  • 3:40 - 3:44
    And I misspelled it,
    and spelling is very important.
  • 3:44 - 3:49
    So that creates an empty `` tag,
    floating off in space.
  • 3:49 - 3:52
    So, first thing we'll do is
    set the text of it,
  • 3:52 - 3:57
    so, `strongEl.textContent = "cat";`.
  • 3:57 - 3:58
    Alright?
  • 3:58 - 4:02
    Alternatively, actually, we could
    do this other thing where
  • 4:02 - 4:05
    we create what's known as a `textNode`.
  • 4:05 - 4:09
    Many DOM nodes in the DOM tree can have
    special types of nodes, `textNode`s,
  • 4:09 - 4:11
    as their children.
  • 4:11 - 4:15
    And these nodes aren't elements, but they
    are still nodes in the DOM tree.
  • 4:15 - 4:18
    We actually call them
    "leaf nodes", because they're
  • 4:18 - 4:20
    the very final thing
    that can be in a tree.
  • 4:20 - 4:22
    [typing]
  • 4:28 - 4:32
    And we'll just pass in the text, "cat".
  • 4:32 - 4:34
    If we use this technique, we've now
  • 4:34 - 4:42
    created two nodes that are floating
    off in space: a `` tag,
  • 4:42 - 4:47
    and then this `textNode`, which
    just says "cat".
  • 4:47 - 4:50
    And we need to connect them
    up to each other.
  • 4:50 - 4:55
    And we want the ``
    to be the parent of "cat".
  • 4:55 - 5:03
    So what we'll do is say
    `strongEl.appendChild(strongText);`.
  • 5:03 - 5:12
    Okay, so now we've got
    the ``with "cat" inside,
  • 5:12 - 5:19
    and we have to append it where we want,
    because it's still floating off in space.
  • 5:19 - 5:25
    We are inside the `for` loop
    for `nameEls`, and each `nameEl`
  • 5:25 - 5:27
    is where we want to append
    the `` tag.
  • 5:27 - 5:35
    So here,
    `nameEls[i].appendChild(strongEl);`.
  • 5:35 - 5:42
    A-ha, and now actually we see it twice,
    because I left in the old way.
  • 5:42 - 5:47
    So it's appending to a `` tag
    that already has a `` in it.
  • 5:47 - 5:52
    We could change this line to
    `innerHTML` equals empty string,
  • 5:52 - 5:58
    which will effectively clear out the span
    before we append to it.
  • 5:58 - 6:05
    Now, as you saw, that took way more lines
    of code than the `innerHTML` version.
  • 6:05 - 6:07
    So, you know, why did we do it?
  • 6:07 - 6:11
    Well, you know, a lot of developers
    don't like modifying the document this way
  • 6:11 - 6:13
    because it does take a lot more code.
  • 6:13 - 6:17
    Most developers actually
    use libraries, like jQuery,
  • 6:17 - 6:21
    to do DOM modification for them,
    which provides functions that
  • 6:21 - 6:27
    does the same code with a lot less
    lines of code for you as the developer,
  • 6:27 - 6:29
    because you're using the
    library functions instead.
  • 6:29 - 6:33
    I do kind of like writing my code
    this way, because I like that
  • 6:33 - 6:39
    I can see exactly how I'm modifying
    the DOM tree, one line at a time.
  • 6:39 - 6:43
    And it feels cleaner than shoving
    everything into a string of innerHTML.
  • 6:43 - 6:45
    But maybe you hate it.
  • 6:45 - 6:47
    Either way, now you know it exists.
  • 6:47 - 6:51
    So you can use it if you need it,
    and you can understand
  • 6:51 - 6:55
    what libraries like jQuery are
    actually doing behind the scenes.
Title:
Creating elements from scratch (Video Version)
Description:

more » « less
Video Language:
English
Duration:
06:56

English subtitles

Revisions