Skip to content
Advertisement

How to add CSS after click through javascript and remove it if not clicked

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.

let plan = document.querySelector('.plan');
let price = document.querySelector('.price');
let item = document.querySelectorAll('.item');



for (var i = 0; i < item.length; i++) {
  item[i].addEventListener('click', showplan, false);
}

function showplan(e) {
  //Originally intended to increase this way, but it seems that it can't be written like this
  // e.target.closesst('li').classList.add('bold')
  // e.target.closesst('h2').classList.add('bold')
  const planSelected = e.target.closest('li').querySelector('h2');
  const priceSelected = e.target.closest('li').querySelector('p');
  plan.textContent = planSelected.textContent;
  price.textContent = priceSelected.textContent;
}
@charset "UTF-8";
.product_list {
  display: flex;
}

.product_list .item {
  border: 1px solid #ccc;
  border-radius: 6px;
  text-align: center;
  padding: 20px;
  margin: 20px;
}

.product_list .item h2 {
  margin-bottom: 16px;
}

.product_list .item:hover {
  border: 1px solid #222;
}

.show {
  border: 2px solid blue;
  padding: 20px;
}

.bold {
  border: 3px solid;
  font-weight: 900;
}
<ul class="product_list">
  <li class="item">
    <h2>coke</h2>
    <p>$100</p>
  </li>
  <li class="item">
    <h2>beef</h2>
    <p>$600</p>
  </li>
</ul>

<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:

item.forEach(el => el.classList.remove('active'))
e.currentTarget.classList.add('active')

Also you have a CSS specificity issue:

when using only .bold, the border would be overriden by .product_list .item

Note, try using currentTarget

const plan = document.querySelector('.plan');
const price = document.querySelector('.price');
const item = document.querySelectorAll('.item');

function showplan(e) {
  const target = e.currentTarget;
  const closestLi = target.closest('li');
  const planSelected = closestLi.querySelector('h2');
  const priceSelected = closestLi.querySelector('p');

  item.forEach(el => el.classList.remove('active'))
  target.classList.add('active')
  plan.textContent = planSelected.textContent;
  price.textContent = priceSelected.textContent;
}

item.forEach(el => el.addEventListener('click', showplan));
@charset "UTF-8";
.product_list {
  display: flex;
}

.product_list .item {
  border: 1px solid #ccc;
  border-radius: 6px;
  text-align: center;
  padding: 20px;
  margin: 20px;
}

.product_list .item h2 {
  margin-bottom: 16px;
}

.product_list .item:hover,
{
  border: 1px solid #222;
}

.product_list .active {
  border: 3px solid red;
  font-weight: 900;
  color: red;
}

.show {
  border: 2px solid blue;
  padding: 20px;
}
<ul class="product_list">
  <li class="item">
    <h2>coke</h2>
    <p>$100</p>
  </li>
  <li class="item">
    <h2>beef</h2>
    <p>$600</p>
  </li>
</ul>

<h2 class="show">Your food of choice is <span class="plan"></span>The total price is<span class="price"></span></h2>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement