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

157
Views
How to find multiple <br> in sequence via document.querySelector?

If I have a sequence of multiple <br>s, how do I display how many sequences do I have?

In other words, I'm trying to count how many times we had <br><br> as in two
s in a row.

// Undesired output:
console.log(document.querySelectorAll('br').length) // 8
console.log(document.querySelectorAll('br+br').length) // 7
<div id="test">
Paragraph 1
<br>
<br>
Paragraph 2
<br>Paragraph 2 continued
<br>
<br>
Paragraph 3
<br>Paragraph 3 continued
<br>
<br>
Paragraph 4
</div>

Desired output:

console.log(document.querySelectorAll('?????').length) // 3
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You may use regex to find <br><br>s:

var p = /<br>[\r\n]*<br>/g;
console.log([...document.querySelector('#test').innerHTML.matchAll(p)].length); // => 3
<div id="test">
Paragraph 1
<br>
<br>
Paragraph 2
<br>Paragraph 2 continued
<br>
<br>
Paragraph 3
<br>Paragraph 3 continued
<br>
<br>
Paragraph 4
</div>

about 4 years ago · Juan Pablo Isaza Report

0

try this:

var nodes = document.querySelectorAll('br, div');
console.log(nodes);
<div id="test">
Paragraph 1
<br>
<br>
Paragraph 2
<br>Paragraph 2 continued
<br>
<br>
Paragraph 3
<br>Paragraph 3 continued
<br>
<br>
Paragraph 4
</div>

about 4 years ago · Juan Pablo Isaza Report

0

I couldn't think of a better way to do it, hopefully this helps!

const brTags = document.querySelectorAll('br')
let tagCount = 0;

for(let i = 0; i < brTags.length; i++){
  const string = brTags[i].nextSibling.textContent
      .replaceAll("\n", "")
      .trim();
  if(string.indexOf("Paragraph") > -1 && string.length === 11){tagCount++}
}

console.log ("Tag count: ", tagCount);
<div id="test">
Paragraph 1
<br>
<br>
Paragraph 2
<br>Paragraph 2 continued
<br>
<br>
Paragraph 3
<br>Paragraph 3 continued
<br>
<br>
Paragraph 4
</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!