Adding Multiple Classes to a Node

Modern JavaScript Manipulating HTML Pages
6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€94.09
List Price:  €131.73
You save:  €37.64
£80.33
List Price:  £112.47
You save:  £32.13
CA$137.95
List Price:  CA$193.14
You save:  CA$55.18
A$155.90
List Price:  A$218.27
You save:  A$62.36
S$136.45
List Price:  S$191.03
You save:  S$54.58
HK$783.08
List Price:  HK$1,096.34
You save:  HK$313.26
CHF 91.31
List Price:  CHF 127.84
You save:  CHF 36.52
NOK kr1,099.13
List Price:  NOK kr1,538.83
You save:  NOK kr439.69
DKK kr702.04
List Price:  DKK kr982.89
You save:  DKK kr280.84
NZ$169.94
List Price:  NZ$237.92
You save:  NZ$67.98
د.إ367.20
List Price:  د.إ514.10
You save:  د.إ146.89
৳10,968.60
List Price:  ৳15,356.49
You save:  ৳4,387.88
₹8,361.11
List Price:  ₹11,705.89
You save:  ₹3,344.78
RM479.15
List Price:  RM670.83
You save:  RM191.68
₦120,104.65
List Price:  ₦168,151.32
You save:  ₦48,046.66
₨27,798.84
List Price:  ₨38,919.49
You save:  ₨11,120.65
฿3,667.03
List Price:  ฿5,133.99
You save:  ฿1,466.96
₺3,248.77
List Price:  ₺4,548.41
You save:  ₺1,299.64
B$518.45
List Price:  B$725.86
You save:  B$207.40
R1,905.92
List Price:  R2,668.37
You save:  R762.44
Лв184.07
List Price:  Лв257.70
You save:  Лв73.63
₩139,257.60
List Price:  ₩194,966.22
You save:  ₩55,708.61
₪377.23
List Price:  ₪528.13
You save:  ₪150.90
₱5,695.28
List Price:  ₱7,973.62
You save:  ₱2,278.34
¥15,458.12
List Price:  ¥21,641.98
You save:  ¥6,183.86
MX$1,680.99
List Price:  MX$2,353.45
You save:  MX$672.46
QR365.97
List Price:  QR512.38
You save:  QR146.40
P1,384.25
List Price:  P1,938
You save:  P553.75
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,851.82
List Price:  E£6,792.74
You save:  E£1,940.92
ብር5,709.83
List Price:  ብር7,993.99
You save:  ብር2,284.16
Kz83,396.59
List Price:  Kz116,758.56
You save:  Kz33,361.97
CLP$97,269.27
List Price:  CLP$136,180.87
You save:  CLP$38,911.60
CN¥723.78
List Price:  CN¥1,013.33
You save:  CN¥289.54
RD$5,930.22
List Price:  RD$8,302.55
You save:  RD$2,372.32
DA13,484.73
List Price:  DA18,879.17
You save:  DA5,394.43
FJ$227.82
List Price:  FJ$318.96
You save:  FJ$91.14
Q778.21
List Price:  Q1,089.53
You save:  Q311.31
GY$20,908.09
List Price:  GY$29,272.17
You save:  GY$8,364.07
ISK kr14,179.58
List Price:  ISK kr19,851.98
You save:  ISK kr5,672.40
DH1,015.88
List Price:  DH1,422.27
You save:  DH406.39
L1,779.82
List Price:  L2,491.82
You save:  L712
ден5,791.90
List Price:  ден8,108.89
You save:  ден2,316.99
MOP$805.91
List Price:  MOP$1,128.31
You save:  MOP$322.39
N$1,902.84
List Price:  N$2,664.06
You save:  N$761.21
C$3,694.12
List Price:  C$5,171.92
You save:  C$1,477.79
रु13,341.25
List Price:  रु18,678.28
You save:  रु5,337.03
S/371.93
List Price:  S/520.72
You save:  S/148.78
K379.86
List Price:  K531.82
You save:  K151.96
SAR375.07
List Price:  SAR525.11
You save:  SAR150.04
ZK2,516.51
List Price:  ZK3,523.21
You save:  ZK1,006.70
L468.24
List Price:  L655.55
You save:  L187.31
Kč2,375.47
List Price:  Kč3,325.75
You save:  Kč950.28
Ft37,163.97
List Price:  Ft52,031.05
You save:  Ft14,867.07
SEK kr1,095.61
List Price:  SEK kr1,533.90
You save:  SEK kr438.29
ARS$86,820.40
List Price:  ARS$121,552.04
You save:  ARS$34,731.63
Bs693.54
List Price:  Bs970.99
You save:  Bs277.44
COP$387,000.37
List Price:  COP$541,816
You save:  COP$154,815.63
₡50,265.75
List Price:  ₡70,374.06
You save:  ₡20,108.31
L2,477.69
List Price:  L3,468.86
You save:  L991.17
₲740,341.68
List Price:  ₲1,036,507.98
You save:  ₲296,166.29
$U3,889.78
List Price:  $U5,445.86
You save:  $U1,556.07
zł407.99
List Price:  zł571.20
You save:  zł163.21
Already have an account? Log In

Transcript

As we've looked at examples of changing the class of a node in the DOM, we've only dealt with adding a single class to an element using the class name attribute, or using the set attribute. Well, HTML allows, including more than one class for a particular element. However, we haven't been able to do that with the methods we have used thus far. html5 introduced a new attribute is called a class list attribute. And when you display class list, it shows you the classes that are associated with a particular element. However, more important are the methods that are included with the class list attribute.

You have an add remove toggle length, and contains method that you can use on class lists. And this allows you to add more than one class to a particular node in the DOM. So if you find the need to do that, or the modifications you're making, you don't want to remove the previous class that is there, you just want to add to it, this would be the technique to use. So let's take a look at the class list attribute and what's available. Now remember, this was added with html5. So make sure you can support html5 in the browsers you're targeting.

Now, the same method for selecting the node is used. Once you have a node selected, you can then use the dot syntax to go to class list. Notice the L is uppercase, then dot again to access the methods that are a part of that. So the first one is add in an inside of parentheses you Put the class name as a string that will be added to that node. Remove, obviously would remove the class name, which you indicate inside a parentheses. Toggle does both add and remove.

So, if you use toggle if the class is already associated with that node, it will remove it. If it is not, it will add it. There is a length property that allows you to see how many classes are actually associated with this node. And then finally, there is a contains method that simply checks to see if the class exists on the note, and it will return either a true or a false. So once again, let's do some examples of these. We'll pull up our selecting exercise HTML page.

And I'm going to open the console and I have the console docs At the bottom of the page, we can see what happens. First off, I'm going to select a node, I'm going to use query selector again and do very similar to what I did in the previous movie. I'm gonna select the first span tag, but I need to type it correctly. I left out the document. reference, you guys probably caught that. Alright, now I have it.

Now let's first access class list. And look, it shows us a list of the classes that are already associated with that node. Now we want to add a new class to that. So I'm going to do node dot class list dot add and then inside of that I'm going to specify a nother class that I've created. This class is very simple, it simply defines a larger font size. That's all it does.

So when I press return, it will change the font size on that span tag. And notice the ID is the only part that's enclosed within the span tag, so we get a very large ind. Alright, let's go ahead and remove that using the class list dot remove method. We have to specify the class name again, and then it removes it. Now toggle, remember I said toggle does both add and remove. If the class already exists, it removes it.

If it doesn't exist, it adds it. So let's try that with our large font class again. So we removed it in our previous state So this should add it. Sure enough, the ID gets larger. Let's execute it again. Press the up arrow to bring the command back, press it again and this time it removes it.

Now, we can see that using the class list length property, and sure enough, we only have one class in there right now. So let's add another class and then we'll check length again. We'll add the large font back in again. And then up arrow to find all classes start linked, press return, and sure enough, we have to there. Now finally, the last method that's available on class list is the contains unless just see if it contains well Blue. No returns true and So sure enough it does.

So the class list added in html5 simply makes it easier to work with classes, because then you can add more than a single class to a node that you have selected from the DOM. The previous methods we we have looked at you're only able to deal with a single class. But there are many times you need to deal with multiple classes, and therefore a class list becomes very valuable. Alright, let's move on to the next topic.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.