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

561
Views
Why does Node.childNodes not return all nodes in vuejs single file component?

From MDN

The Node.childNodes read-only property returns a live NodeList of child nodes of the given element where the first child node is assigned index 0. Child nodes include elements, text and comments.

Which works as expected

Jsfiddle

btn.addEventListener('click', ()=>{
    console.log(container.childNodes)
})
  <div id="container">
    <span>A</span>
    <span>B</span>
    <span>C</span>
  </div>
  <button id='btn'>Log Nodes</button>

0: "#text"
1: <span>
2: "#text"
3: ...
length: 7

This logs every node including text nodes, etc.

But when used in Vue it only logs elements without the text nodes inside.

1: <span>
2: <span>
3: <span>
length: 3

Codesandbox with vue

I know you're not suppose to access elements like this in Vue, but I'm wondering if this is normal behavior and why it happens. And how do I get all the nodes inside an element in Vue ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is very tricky to compare normal static HTML and what Vue renders because it's "apples vs pears" comparison.

The template you give to a Vue may look like HTML but Vue don't use it as HTML text. It parses the template and produce render function which is just JavaScript code, that generates VNode's (virtual DOM)

And the thing is that during the parsing, Vue tends to ignore all insignificant whitespace by default.

There is a compiler setting that controls the whitespace handling. While default value condense removes most of the whitespace, value preserve will preserve some.

You can compare the difference in online Vue template explorer:

  1. condense
  2. preserve
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!