< Return to Video

Changing attributes (Video Version)

  • 0:00 - 0:06
    I'm back with my webpage about dogs,
    and I am very determined to use JavaScript
  • 0:06 - 0:12
    and the DOM api to turn it into a
    webpage entirely about cats instead.
  • 0:12 - 0:17
    There is an elephant in the room that I've
    been ignoring. Well, actually, there's
  • 0:17 - 0:24
    a dog in the room; two dogs, in fact--
    these images. I can't have images
  • 0:24 - 0:29
    of these adorable dogs on my page about
    adorable cats. I need to change them.
  • 0:29 - 0:36
    So, let's start by finding the images,
    using getElementsByTagName.
  • 0:36 - 0:48
    `var imageEls =
    document.getElementsByTagName("img");`
  • 0:48 - 0:51
    Now, since that returns multiple elements,
  • 0:51 - 0:55
    we need to use a for loop to iterate
    through them, so I'll set that up.
  • 0:55 - 1:04
    `var i = 0; i < imageEls.length; i++`
  • 1:04 - 1:11
    But what do I put inside the for loop?
    I can't change image elements with
  • 1:11 - 1:19
    innerHTML because image tags don't have
    an innerHTML. They're autoclosing tags.
  • 1:19 - 1:24
    Instead, I need to change the thing about
    them that makes them look like dogs--
  • 1:24 - 1:27
    the URL of the pictures,
    the thing that is specified
  • 1:27 - 1:31
    with the src attribute
    in each of the tags.
  • 1:31 - 1:36
    We can change attributes of elements
    using dot notation. Let me show you.
  • 1:36 - 1:43
    First we access the current element with
    bracket notation, and then we say dot
  • 1:43 - 1:51
    and we put the HTML attribute name as the
    JavaScript property name-- src-- equals
  • 1:51 - 1:55
    and then we set it to some new value;
    and I'll just put an empty string.
  • 1:55 - 1:57
    Notice the images went blank,
  • 1:57 - 2:02
    since an empty string
    doesn't point at any image at all.
  • 2:02 - 2:08
    To figure out what this new URL should be,
    I'm going to paste the old URL here
  • 2:08 - 2:13
    and change the file name to cat,
    because I happen to know
  • 2:13 - 2:17
    that that's the URL of the
    cat image on Khan Academy.
  • 2:17 - 2:23
    Yay. The dogs are now cats, and
    our catification is mostly complete.
  • 2:23 - 2:31
    Do you see anything else that still has to
    do with dogs? It's really subtle, but
  • 2:31 - 2:37
    there is one thing left-- the link to
    Wikipedia. It's going to the dogs page
  • 2:37 - 2:41
    and if the viewer follows it, they're
    going to catch on to my nasty trick.
  • 2:41 - 2:47
    So I want to send them to cats instead.
    How should I find that link in JavaScript?
  • 2:47 - 2:52
    I could give this link an ID
    and use getElementById.
  • 2:52 - 2:57
    Or I could use getElementsByTagName
    and change all the links on the page.
  • 2:57 - 2:59
    Or I could be really super fancy,
  • 2:59 - 3:07
    and find only links that go to pages
    about dogs, using a CSS query selector.
  • 3:07 - 3:12
    Let me show you the CSS query that I want
    to do up here in the tag first.
  • 3:12 - 3:16
    So this CSS query is going to find all
    links that have to do with dogs.
  • 3:16 - 3:19
    First I'm going to type `a`,
    'cause I'm just looking for links.
  • 3:19 - 3:26
    Then I'm going to say `[href*="Dog"]`.
  • 3:26 - 3:32
    So this tells CSS to match
    any link that has "Dog" in it.
  • 3:32 - 3:38
    And then we'll set the color to purple.
    Ta da, it selected it.
  • 3:38 - 3:44
    So, that's pretty neat, and this is an
    attribute selector, and there's a lot of
  • 3:44 - 3:49
    neat ways to use attribute selectors to
    really dig deep into your document.
  • 3:49 - 3:58
    So now to find that in JavaScript, we can
    use document.querySelectorAll, so I'll say
  • 3:58 - 4:08
    `var linkEls = document.querySelectorAll`,
    and then we can just go and paste in the
  • 4:08 - 4:13
    selector that we just made, although
    we do have to make sure that
  • 4:13 - 4:23
    we escape our double quotes. There we go.
    So now it looks like a good string.
  • 4:23 - 4:28
    And now I need to iterate through these,
    so once again we just do our for loop--
  • 4:28 - 4:35
    should be getting really used to
    doing these for loops-- and for each of
  • 4:35 - 4:42
    them I want to change the link to the
    webpage about cats on Wikipedia.
  • 4:42 - 4:46
    So I'm going to say `linkEls[i].href`--
  • 4:46 - 4:51
    because that's the name of the attribute
    that we're changing-- and then equals,
  • 4:51 - 5:00
    then I'll go find this URL and paste it
    down here and just change it to cat,
  • 5:00 - 5:04
    because I'm pretty sure
    that's the URL of that page.
  • 5:04 - 5:11
    Ta da, world catination is complete.
    But you are not done
  • 5:11 - 5:31
    learning how to manipulate webpages
    with JavaScript, so stick around.
Title:
Changing attributes (Video Version)
Description:

more » « less
Video Language:
English
Duration:
05:31
  • Where "<style>" was used in the captions, it shows up in the transcript, but not in the video viewer here on Amara. Will it show up properly on Youtube or KA?

English subtitles

Revisions