Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!