Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
Hiding all instances of 2 divs depending on contents of a string

I have a custom Wordpress blog post page that is showing personnel profile cards, 12 people at a time. Sometimes, people have a Twitter handle, others not. When the value entered in the backend for their handle is "none", both the title and the Twitter field should be hidden, for ALL people on the same screen (again, paginating through 12 people at a time). Otherwise, it should show both.

<p>Sample text</p>
<div class="twitter-handle-title">
  <p>Twitter Handle:</p>
</div>
<div class="twitter-handle-text">
  <p>@twitter</p>
</div>


<p>Sample text.</p>
<div class="twitter-handle-title">
  <p>Twitter Handle:</p>
</div>
<div class="twitter-handle-text">
   <p>None</p>
</div>

This is the javascript I was trying to use, but doesn't work:

let txt = document.getElementsByClassName("twitter-handle-text").innerText;
if (txt == "None") {
    document.getElementsByClassName('twitter-handle-title').style.display = "none";
    document.getElementsByClassName('twitter-handle-text').style.display = "none";
}

Any guidance would be appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As mentioned, getElementsByClassName returns a HTMLCollection of all of the targeted elements.

To apply your intended logic to each profile card you will need to iterate over each one and run your checks and style updates on each iteration.

To make this easier, I'd suggest adding a wrapper to each card element in order to encapsulate your logic. Something like this:

<div class="profile-card">
  <p>Sample text.</p>
  <div class="twitter-handle-title">
    <p>Twitter Handle:</p>
  </div>
  <div class="twitter-handle-text">
    <p>None</p>
  </div>
</div>

Now you can select all of the card wrappers using a document interface such as getElementsByClassName or querySelectorAll.

const cards = document.querySelectorAll('.profile-card');

Once we have a collection of all of the profile-card elements, we can loop over each wrapping element using the forEach array method. Now that we are encapsulated within each card we can apply your logic.

For the inner selectors I'd recommend using querySelector as we only need to select a single element by class name rather than a collection.

cards.forEach((card) => {
  if (card.querySelector('.twitter-handle-text').innerText === 'None') {
    card.querySelector('.twitter-handle-title').style.display = 'none';
    card.querySelector('.twitter-handle-text').style.display = 'none';
  }
})

Hope this helps!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda