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

261
Vistas
Adding a class based on the first first of the string in jQuery

I am trying to add a class to a div based on the first word of the string. e.g

<div id="am-event-138 " class="am-event">
 <div class="am-event-title">Ballet (Live stream from School Ballet)</div>
</div>

<div id="am-event-139" class="am-event">
 <div class="am-event-title">Ballet</div>
</div>

<div id="am-event-140" class="am-event">
 <div class="am-event-title">Dance (Live stream from School Ballet)</div>
</div>

In the above, if the first word of the event title is Ballet, it should add the class ballet to am-event, if Dance then dance to am-event.

I am trying the following but the code does not seem to work as expected as it seems to take an occurrence of the word in the string rather than just the first word:

$(document).ready(function() {
  $(".am-event").each(function() {
    if (0 <= $(this).text().indexOf("Ballet")) {
      $(this).addClass("ballet");
    } else if (0 <= $(this).text().indexOf("Dance")) {
      $(this).addClass("dance");

    } else {}
  })
})
.ballet {
  background: red;
}

.dance {
  background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Trim the text so you can see if it startsWith either of those words - much more elegant (and semantically appropriate and understandable) than comparing against an indexOf result.

$(document).ready(function() {
  $(".am-event").each(function() {
    const text = $(this).text().trim();
    if (text.startsWith("Ballet")) {
      $(this).addClass("ballet");
    } else if (text.startsWith("Dance")) {
      $(this).addClass("dance");
    }
  })
})
.ballet {
  background: red;
}

.dance {
  background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Alternative, you can also use split(' ')[0] and compare text and based upon that add your class.

var b = "Ballet";
var d = "Dance"
$(".am-event-title").each(function() {
  var text = $(this).text().trim().split(' ')[0];
  if (text == b) {
    $(this).parent('div').addClass("ballet");
  } else if (text == d) {
    $(this).parent('div').addClass("dance");
  }
})
.ballet {
  background: red;
}

.dance {
  background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to trim off the whitespace. You can then use startsWith.

$(document).ready(function() {
  $(".am-event").each(function() {
    const text = $(this).text().trim();
    if (text.startsWith('Ballet')) {
      $(this).addClass('ballet');
    }
    if (text.startsWith('Dance')) {
      $(this).addClass('dance');
    }
  });
})
.ballet { background: red; }
.dance { background: lightblue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

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