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

213
Visualizações
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 Respostas
Responde à pergunta

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