I have encountered a problem with javascript clicking to change CSS!
I hope that the outer frame and text of the clicked option can be thickened after clicking, but the ones that are not selected should not be thickened! I have tried several ways of writing, but I still don’t know how To complete this requirement, I hope to get everyone’s help, thank you.
JavaScript
x
19
19
1
let plan = document.querySelector('.plan');
2
let price = document.querySelector('.price');
3
let item = document.querySelectorAll('.item');
4
5
6
7
for (var i = 0; i < item.length; i++) {
8
item[i].addEventListener('click', showplan, false);
9
}
10
11
function showplan(e) {
12
//Originally intended to increase this way, but it seems that it can't be written like this
13
// e.target.closesst('li').classList.add('bold')
14
// e.target.closesst('h2').classList.add('bold')
15
const planSelected = e.target.closest('li').querySelector('h2');
16
const priceSelected = e.target.closest('li').querySelector('p');
17
plan.textContent = planSelected.textContent;
18
price.textContent = priceSelected.textContent;
19
}
JavaScript
1
30
30
1
@charset "UTF-8";
2
.product_list {
3
display: flex;
4
}
5
6
.product_list .item {
7
border: 1px solid #ccc;
8
border-radius: 6px;
9
text-align: center;
10
padding: 20px;
11
margin: 20px;
12
}
13
14
.product_list .item h2 {
15
margin-bottom: 16px;
16
}
17
18
.product_list .item:hover {
19
border: 1px solid #222;
20
}
21
22
.show {
23
border: 2px solid blue;
24
padding: 20px;
25
}
26
27
.bold {
28
border: 3px solid;
29
font-weight: 900;
30
}
JavaScript
1
12
12
1
<ul class="product_list">
2
<li class="item">
3
<h2>coke</h2>
4
<p>$100</p>
5
</li>
6
<li class="item">
7
<h2>beef</h2>
8
<p>$600</p>
9
</li>
10
</ul>
11
12
<h2 class="show">Your food of choice is <span class="plan"></span>The total price is<span class="price"></span></h2>
Advertisement
Answer
You need to toggle the class, you can do that by removing it from every item and only setting it to the selected one:
JavaScript
1
3
1
item.forEach(el => el.classList.remove('active'))
2
e.currentTarget.classList.add('active')
3
Also you have a CSS specificity issue:
when using only .bold
, the border
would be overriden by .product_list .item
Note, try using currentTarget
JavaScript
1
17
17
1
const plan = document.querySelector('.plan');
2
const price = document.querySelector('.price');
3
const item = document.querySelectorAll('.item');
4
5
function showplan(e) {
6
const target = e.currentTarget;
7
const closestLi = target.closest('li');
8
const planSelected = closestLi.querySelector('h2');
9
const priceSelected = closestLi.querySelector('p');
10
11
item.forEach(el => el.classList.remove('active'))
12
target.classList.add('active')
13
plan.textContent = planSelected.textContent;
14
price.textContent = priceSelected.textContent;
15
}
16
17
item.forEach(el => el.addEventListener('click', showplan));
JavaScript
1
32
32
1
@charset "UTF-8";
2
.product_list {
3
display: flex;
4
}
5
6
.product_list .item {
7
border: 1px solid #ccc;
8
border-radius: 6px;
9
text-align: center;
10
padding: 20px;
11
margin: 20px;
12
}
13
14
.product_list .item h2 {
15
margin-bottom: 16px;
16
}
17
18
.product_list .item:hover,
19
{
20
border: 1px solid #222;
21
}
22
23
.product_list .active {
24
border: 3px solid red;
25
font-weight: 900;
26
color: red;
27
}
28
29
.show {
30
border: 2px solid blue;
31
padding: 20px;
32
}
JavaScript
1
12
12
1
<ul class="product_list">
2
<li class="item">
3
<h2>coke</h2>
4
<p>$100</p>
5
</li>
6
<li class="item">
7
<h2>beef</h2>
8
<p>$600</p>
9
</li>
10
</ul>
11
12
<h2 class="show">Your food of choice is <span class="plan"></span>The total price is<span class="price"></span></h2>