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

212
Vistas
Want to get the length condition of innerText for the number of instances not the number of characters

I have: document.querySelectorAll('div.stops') that appears with innerText "Direct" twice on the page.

So I want to apply a condition that if the text "Direct" appears twice, then do something. I have a code like below, but it does not give me the length of the number of times the word "Direct" appears. Instead, it just gives me the number of characters, which is 6.

for(let i=0; i < document.querySelectorAll('div.stops').length; i++){
if (document.querySelectorAll('div.stops')[i].innerText == 'Direct'){
// Do something
}}

Image where the text "Direct" appears 2 times

Image where the text "Direct" appears 2 times

Screenshot of the console where I tried to find the length

Screenshot of the console where I tried to find the length

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

0

  1. Cache your elements. Use Array.from to create an array from the "array-like" list of nodes that querySelectorAll returns. That way you have access to array methods like filter.

  2. filter out the elements that have 'Direct' as their textContent. Note: filter returns a new array.

  3. If the length of that array is 2 do the thing.

const stops = document.querySelectorAll('.stops');

const direct = Array.from(stops).filter(stop => {
  return stop.textContent === 'Direct';
});

if (direct.length === 2) {
  console.log('Good');
}
<div class="stops">Direct</div>
<div class="stops">Karen</div>
<div class="stops">Indirect</div>
<div class="stops">Bus</div>
<div class="stops">Banana</div>
<div class="stops">Direct</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