I have a few variables with colors that I want to change later. I want certain class to have background color from that variable. Every object with that class should be that color (nav, footer etc.). I have something like this, but it doesn’t work. Can you help?
JavaScript
x
43
43
1
// Set colors below:
2
3
var nhcp1 = "red"; // Color 1
4
var nhcp2 = "blue"; // Color 2
5
var nhcp3 = "pink"; // Color 3
6
var nhcp4 = "green"; // Color 4
7
var nhcp5 = "violet"; // Color 5
8
9
var d = document;
10
11
12
13
// Functions
14
15
16
window.onload = function () {
17
18
// Functions for Color 1 ===============================
19
20
d.querySelector(".nhcp1").style.backgroundColor = nhcp1;
21
22
// Functions for Color 2 ===============================
23
24
d.querySelector(".nhcp2").style.backgroundColor = nhcp2;
25
26
// Functions for Color 3 ===============================
27
28
d.querySelector(".nhcp3").style.backgroundColor = nhcp3;
29
30
// Functions for Color 4 ===============================
31
32
d.querySelector(".nhcp4").style.backgroundColor = nhcp4;
33
34
// Functions for Color 5 ===============================
35
36
d.querySelector(".nhcp5").style.backgroundColor = nhcp5;
37
38
};
39
40
41
42
43
Advertisement
Answer
in fact I didn’t see any problem, but you can also coding that this way
JavaScript
1
12
12
1
const
2
d = document
3
, colors =
4
{ nhcp1 : 'red' // Color 1
5
, nhcp2 : 'blue' // Color 2
6
, nhcp3 : 'pink' // Color 3
7
, nhcp4 : 'green' // Color 4
8
, nhcp5 : 'violet' // Color 5
9
}
10
for (let color in colors )
11
d.querySelectorAll(`.${color}`)
12
.forEach(el=>el.style.backgroundColor = colors[color])
JavaScript
1
17
17
1
<div class="nhcp1">bg 1</div>
2
<div class="nhcp2">bg 2</div>
3
<div class="nhcp3">bg 3</div>
4
<div class="nhcp4">bg 4</div>
5
<div class="nhcp5">bg 5</div>
6
7
<div class="nhcp1">bg 1</div>
8
<div class="nhcp2">bg 2</div>
9
<div class="nhcp3">bg 3</div>
10
<div class="nhcp4">bg 4</div>
11
<div class="nhcp5">bg 5</div>
12
13
<div class="nhcp1">bg 1</div>
14
<div class="nhcp2">bg 2</div>
15
<div class="nhcp3">bg 3</div>
16
<div class="nhcp4">bg 4</div>
17
<div class="nhcp5">bg 5</div>
, how can I modify it so for example class nhcp1txt will make text nhcp1 color (of course I want previous background color too, I want to add another class)?
this way :
JavaScript
1
15
15
1
const
2
d = document
3
, colors =
4
[ { cls: 'nhcp1', cr: 'red', st:'backgroundColor' }
5
, { cls: 'nhcp2', cr: 'blue', st:'backgroundColor' }
6
, { cls: 'nhcp3', cr: 'pink', st:'backgroundColor' }
7
, { cls: 'nhcp4', cr: 'green', st:'backgroundColor' }
8
, { cls: 'nhcp5', cr: 'violet', st:'backgroundColor' }
9
, { cls: 'nhcp1txt', cr: 'blue', st:'color' }
10
, { cls: 'nhcp2txt', cr: 'yellow', st:'color' }
11
];
12
13
for (let {cls, cr, st} of colors )
14
d.querySelectorAll(`.${cls}`)
15
.forEach( el=> el.style[st] = cr )
JavaScript
1
11
11
1
<div class="nhcp1 nhcp1txt">bg 1</div>
2
<div class="nhcp2 nhcp2txt">bg 2</div>
3
<div class="nhcp3">bg 3</div>
4
<div class="nhcp4">bg 4</div>
5
<div class="nhcp5">bg 5</div>
6
7
<div class="nhcp1">bg 1</div>
8
<div class="nhcp2">bg 2</div>
9
<div class="nhcp3 nhcp1txt">bg 3</div>
10
<div class="nhcp4">bg 4</div>
11
<div class="nhcp5 nhcp2txt">bg 5</div>