i want to make the color of the number green if its more than zero to make it green and want to be red when its below zero but i cant do it i dont get why can anyone explain
JavaScript
x
44
44
1
const button1 = document.querySelector('.prevBtn');
2
const button2 = document.querySelector('.nextBtn');
3
const main = document.querySelector('.main-container')
4
const count = document.getElementById('counter')
5
6
let counter = 0
7
8
9
10
main.addEventListener('click', e => {
11
if(e.target.classList.contains('nextBtn')){
12
13
counter++
14
15
16
html=`
17
<h1 class="text-uppercase">counter</h1>
18
<h1 id="counter"class =count">${counter}</h1>
19
<div class="btn-container d-flex justify-content-around flex-wrap">
20
<button class="btn counterBtn prevBtn text-uppercase m-2">lower count</button>
21
<button class="btn counterBtn nextBtn text-uppercase m-2">add count</button>
22
`
23
main.innerHTML = html
24
25
}else if(e.target.classList.contains('prevBtn')){
26
counter--
27
28
html=`
29
<h1 class="text-uppercase">counter</h1>
30
<h1 id="counter">${counter}</h1>
31
<div class="btn-container d-flex justify-content-around flex-wrap">
32
<button class="btn counterBtn prevBtn text-uppercase m-2">lower count</button>
33
<button class="btn counterBtn nextBtn text-uppercase m-2">add count</button>
34
`
35
main.innerHTML = html
36
}
37
})
38
39
if(counter > 0){
40
count.style.color = 'green'
41
} else if(counter < 0) {
42
count.style.color = 'red'
43
}
44
Advertisement
Answer
The order in your code is not correct. You must get an element after it is added to DOM.
Try the snippet.
JavaScript
1
38
38
1
const main = document.querySelector('.main-container')
2
3
let counter = 0
4
5
main.addEventListener('click', e => {
6
if(e.target.classList.contains('nextBtn')){
7
counter++
8
html=`
9
<h1 class="text-uppercase">counter</h1>
10
<h1 id="counter"class =count">${counter}</h1>
11
<div class="btn-container d-flex justify-content-around flex-wrap">
12
<button class="btn counterBtn prevBtn text-uppercase m-2">lower count</button>
13
<button class="btn counterBtn nextBtn text-uppercase m-2">add count</button>
14
`;
15
main.innerHTML = html
16
17
}else if(e.target.classList.contains('prevBtn')) {
18
counter--
19
html=`
20
<h1 class="text-uppercase">counter</h1>
21
<h1 id="counter">${counter}</h1>
22
<div class="btn-container d-flex justify-content-around flex-wrap">
23
<button class="btn counterBtn prevBtn text-uppercase m-2">lower count</button>
24
<button class="btn counterBtn nextBtn text-uppercase m-2">add count</button>
25
`;
26
main.innerHTML = html
27
}
28
29
const button1 = document.querySelector('.prevBtn');
30
const button2 = document.querySelector('.nextBtn');
31
const count = document.getElementById('counter')
32
33
if(counter > 0){
34
count.style.color = 'green'
35
} else if(counter < 0) {
36
count.style.color = 'red'
37
}
38
});
JavaScript
1
4
1
.main-container {
2
width: 100%;
3
height: 500px;
4
}
JavaScript
1
1
1
<div class="main-container nextBtn">Click On Me</div>