Return to Video

Changing CSS classes

  • 0:01 - 0:06
    In the last talk-through, I showed how
    to set styles in JavaScript, like this.
  • 0:06 - 0:10
    Now, if you set a lot of styles
    in JavaScript, you could end up
  • 0:10 - 0:15
    with hundreds of lines of code
    just setting CSS property values.
  • 0:15 - 0:18
    And that can really
    clutter up your JavaScript.
  • 0:18 - 0:21
    There's another way we could do this.
  • 0:21 - 0:23
    We could create a CSS class,
  • 0:23 - 0:28
    and add a CSS rule for that class
    with the new styles that we want,
  • 0:28 - 0:33
    and then dynamically
    add that class name in JavaScript.
  • 0:33 - 0:39
    Let's start up here at the `` tag
    and try this out.
  • 0:39 - 0:46
    We'll make a `.catcolors` class,
    and the CSS rule for it.
  • 0:46 - 0:53
    And it will have `color: orange`,
    and `background-color: black`.
  • 0:53 - 0:59
    Now to assign that to the
    `heading` element, we can say:
  • 0:59 - 1:06
    `headingEl.className = "catcolors";
  • 1:06 - 1:09
    And we can delete
    the assignment of the color
  • 1:09 - 1:12
    and background color in JavaScript.
  • 1:12 - 1:13
    Ta-da!
  • 1:13 - 1:15
    So that worked.
  • 1:15 - 1:17
    Now, notice something weird?
  • 1:17 - 1:21
    The HTML attribute
    for class names is just `class`.
  • 1:21 - 1:27
    If I had done this in HTML,
    it would have been `class="catcolors"`.
  • 1:27 - 1:32
    But when I did it in JavaScript,
    I had to say `.className`,
  • 1:32 - 1:34
    which is not what we're used to.
  • 1:34 - 1:39
    And that is because `class` is actually
    a keyword in the JavaScript language
  • 1:39 - 1:43
    which has a special meaning
    for the language.
  • 1:43 - 1:48
    So because of that, browsers decided
    to use `className` to refer
  • 1:48 - 1:52
    to the HTML class attribute, just to
    make sure they wouldn't get confused.
  • 1:52 - 1:57
    So remember, if you want to set
    the class attribute of an element,
  • 1:57 - 2:00
    you say dot className equals.
  • 2:00 - 2:04
    Now to assign that to each
    of the animal names,
  • 2:04 - 2:07
    we can put it inside the loop, so:
  • 2:07 - 2:13
    `nameEls[i].className = "catColors";`
  • 2:13 - 2:19
    That will add the class name,
    but actually it will remove any classes
  • 2:19 - 2:24
    that were there before,
    because we said equals.
  • 2:24 - 2:28
    So if we had any classes there before,
    those are now gone.
  • 2:28 - 2:32
    Now, they did have classes before,
    class equals animal.
  • 2:32 - 2:34
    And so that's become catColors.
  • 2:34 - 2:41
    So what we really want to do, is add
    a new class name to this class attribute.
  • 2:41 - 2:48
    And we can do that by saying
    plus equals space catColors.
  • 2:48 - 2:49
    There we go.
  • 2:49 - 2:52
    So that's the safe thing to do,
    because it will take
  • 2:52 - 2:58
    whatever the previous class was,
    add a space and then the new class to it.
  • 2:58 - 3:02
    There is another way to do this
    in newer browsers,
  • 3:02 - 3:05
    using the `classList` property.
  • 3:05 - 3:15
    So to do that, we say:
    `nameEls[i].classList.add("catcolors");`.
  • 3:15 - 3:20
    Now that's a lot nicer,
    but doesn't work everywhere.
  • 3:20 - 3:24
    So if you do want to use that,
    you have to go to caniuse.com,
  • 3:24 - 3:27
    and make sure it's supported
    in all the browsers
  • 3:27 - 3:29
    that you want your webpage to work in.
  • 3:29 - 3:32
    It is a lot nicer, but it's not nice
    if your webpage breaks
  • 3:32 - 3:36
    because you're using a function
    that the browser doesn't know about.
  • 3:36 - 3:38
    So I'll just comment it out.
  • 3:38 - 3:41
    Because I want mine working
    in a lot of browsers.
  • 3:41 - 3:46
    There are still lots of times when we
    want to change individual styles,
  • 3:46 - 3:49
    instead of assigning class names.
  • 3:49 - 3:54
    So once again, just remember that you have
    both of these tools in your toolbox,
  • 3:54 - 3:59
    and think about what might be best
    depending on the situation.
Title:
Changing CSS classes
Description:

more » « less
Video Language:
English
Duration:
04:00

English subtitles

Revisions