Return to Video

Finding elements by CSS selector (Video Version)

  • 0:02 - 0:06
    You've now got a lot of tools
    in your toolbox for finding elements.
  • 0:06 - 0:09
    And those will work great
    a lot of the time.
  • 0:09 - 0:12
    But there is one final tool
    that's the most powerful of them all:
  • 0:12 - 0:18
    the ability to find elements
    based on any CSS selector.
  • 0:18 - 0:20
    Do you remember CSS selectors?
  • 0:20 - 0:25
    The basic ones were
    finding by tag name,
  • 0:25 - 0:26
    or ID
  • 0:26 - 0:28
    or class
  • 0:28 - 0:30
    But there are many
    more advanced selectors:
  • 0:30 - 0:35
    descendant selectors, attribute selectors,
    combined class plus element selectors--
  • 0:35 - 0:39
    this would be a good time
    for you to review CSS selectors
  • 0:39 - 0:41
    if you've forgotten all of those.
  • 0:41 - 0:46
    For example, what if I wanted
    to specifically style only the elements
  • 0:46 - 0:51
    with class name "animal"
    that are inside a paragraph?
  • 0:51 - 0:55
    Let's stick a `` tag in
    and do that.
  • 0:55 - 0:57
    So I would first write `p`,
  • 0:57 - 1:02
    and then a space, to say that
    I'm looking inside ``s,
  • 1:02 - 1:06
    and then dot animal to say that
    I'm looking for any elements
  • 1:06 - 1:10
    with class name "animal".
  • 1:10 - 1:13
    And I'll just color them red.
  • 1:13 - 1:14
    Beautiful.
  • 1:14 - 1:20
    Now, I could use the same CSS selector
    to find those elements in JavaScript,
  • 1:20 - 1:25
    using the
    `document.querySelectorAll()` method.
  • 1:25 - 1:30
    So I'll change this line here.
  • 1:30 - 1:35
    And I need to pass the CSS selector
    as the argument,
  • 1:35 - 1:38
    as a string in quotes.
  • 1:38 - 1:42
    And there, the paragraph
    is about cats again.
  • 1:42 - 1:46
    You could pass any valid
    CSS selector to that function,
  • 1:46 - 1:48
    and it will return back
    a collection of elements
  • 1:48 - 1:51
    that you can then loop through.
  • 1:51 - 1:54
    Do you remember last time
    how we said
  • 1:54 - 1:58
    that `getElementsByTagName()`
    returns an HTML collection
  • 1:58 - 2:00
    that looks a lot like an array?
  • 2:00 - 2:07
    Well this method returns a `NodeList`,
    which is also a lot like an array,
  • 2:07 - 2:13
    in that we can use the brackets on it,
    we can check the length, all of that.
  • 2:13 - 2:16
    And you probably won't run into
    the differences between
  • 2:16 - 2:20
    a NodeList and an HTMLCollection
    when you're using these methods.
  • 2:20 - 2:24
    But you are welcome to look them up
    if you'd like to learn more about them.
  • 2:24 - 2:29
    Okay, now, there's also a method
    that will look up CSS selectors,
  • 2:29 - 2:33
    and return only one element,
    if you know your CSS selector
  • 2:33 - 2:35
    is only selecting one thing.
  • 2:35 - 2:37
    In practice, it's not really as useful,

  • 2:37 - 2:40
    because you usually have
    an ID that you can use in that case.
  • 2:40 - 2:43
    But I'll show it to you just in case.
  • 2:43 - 2:46
    So here, instead of `getElementById`,
    I could actually say:
  • 2:46 - 2:49
    `document.querySelector("`
  • 2:49 - 2:54
    and then for the actual query,
    to make it be an ID, I just add that hash.
  • 2:54 - 2:55
    Ta-da.
  • 2:55 - 2:58
    So, as you see, this is really similar
    to `querySelectorAll()`,
  • 2:58 - 3:02
    except we're only selecting one,
    so it's just `querySelector()`,
  • 3:02 - 3:07
    and we just pass in some CSS selector
    that we expect to get back one element.
  • 3:07 - 3:10
    So when should you use
    each of these tools in your toolbox?
  • 3:10 - 3:14
    The first ones that I showed you
    tend to perform better.
  • 3:14 - 3:16
    So I suggest using those when you can.
  • 3:16 - 3:20
    But if you're in a situation where
    you need to use a complex CSS selector,
  • 3:20 - 3:24
    and you can't use those,
    then `querySelectorAll()` is your friend.
  • 3:24 - 3:26
    Try it out in the next challenge,
  • 3:26 - 3:31
    then get ready to see way more ways
    that you can manipulate this webpage.
Title:
Finding elements by CSS selector (Video Version)
Description:

more » « less
Video Language:
English
Duration:
03:33

English subtitles

Revisions