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

379
Visualizações
How to use javascript to check if class that starts with (classname__) exists?

In the snippet below, I'm using the contains() method to check if a class that starts with banner__ exists in the document. If I define the class explicitly, then I can get a true response. But there are various classes that could exist in the document that start with banner__.

How do I check if a class that starts with banner__ exists?

const banner = document.querySelector('.banner');
const isBanner = banner.classList.contains('banner__');
if (isBanner) {
  console.log('Exists')
} else {
  console.log('Does not exist')
}
<div class="banner banner__holiday">Banner</div>

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

0

Convert the class list to an array, then use .some() to test if any of them begin with what you want.

const banner = document.querySelector('.banner');
const isBanner = Array.from(banner.classList).some(c => c.startsWith('banner__'));
if (isBanner) {
  console.log('Exists')
} else {
  console.log('Does not exist')
}
<div class="banner banner__holiday">Banner</div>

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