H
H
heisenberg12021-06-23 22:51:12
JavaScript
heisenberg1, 2021-06-23 22:51:12

How to apply a class to all child elements?

Can you tell me how to apply the class to all child elements?
The data comes from the server and it is necessary that the block is hidden if there is not enough data (i.e. if there is no data, then the block for this data is hidden)

At the moment I add hidden for each block separately, and then I remove hidden from each block separately when the conditions are met, but I understand what is needed and it is possible to optimize all this
Part of the code:

relatedOffers.forEach(({suggestions}) => {
  const advert = template.cloneNode(true);
  advert.classList.add('hidden');
  const popupTitle = advert.querySelector('.popup__title');
  if (suggestions.title.length > 0) {
    popupTitle.classList.remove('hidden');
    popupTitle.textContent = suggestions.title;
  }

  const popupAddress = advert.querySelector('.popup_address');
  if (suggestions.address.length > 0) {
    popupAddress.classList.remove('hidden');
    popupAddress.textContent = suggestions.address;
  }

  const popupDescription = advert.querySelector('.popup_description');
  if (suggestions.description.length > 0) {
    popupDescription.classList.remove('hidden');
    popupDescription.textContent = suggestions.description;
  }

  const popupFeatures = advert.querySelector('.popup_features');
  while (popupFeatures.firstChild) { 
    popupFeatures.removeChild(popupFeatures.firstChild);
  }
  for (let ind = 0; ind < suggestions.features.length; ind++) {
    const feature = document.createElement('li');
    feature.classList.add('popup_feature');
    feature.classList.add(`popup_feature--${suggestions.features[ind]}`);
    popupFeatures.appendChild(feature);
  }

  const popupPrice = advert.querySelector('.popup_price');
  if (suggestions.price > 0) {
    popupPrice.classList.remove('hidden');
    popupPrice.textContent = suggestions.price;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Dubrovin, 2021-06-24
@alekcena

document.querySelector / querySelectorAll ( По 1 элементу / По всем элементам )
//Ищет как CSS через селекторы

To apply styles to all child elements in CSS
.element > *{

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question