Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda