What could be the reason for Node.childNodes not displaying all nodes within a Vue.js single file component?

Referenced from MDN

The Node.childNodes property obtains a dynamic NodeList that consists of all child nodes within the specified element, starting with index 0. Child nodes can comprise elements, text, and comments.

This functionality performs as anticipated

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.

However, when utilized in Vue, it solely logs elements but not the accompanying text nodes inside them.

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

Codesandbox with vue

I acknowledge that accessing elements in this manner is not recommended in Vue, yet I am curious if this behavior is standard and the reasoning behind it. Also, how can I fetch all nodes contained within an element in Vue?

Answer №1

Comparing traditional static HTML with what Vue renders can be quite challenging because it's like comparing "apples vs pears."

The template provided to Vue may resemble HTML, but Vue doesn't treat it as plain HTML text. Instead, it parses the template and generates a render function, which is essentially JavaScript code that creates VNodes (virtual DOM).

During parsing, Vue tends to disregard insignificant whitespace by default.

There exists a compiler setting that dictates how whitespace should be handled. The default value of `condense` removes most whitespace, while the value of `preserve` retains some.

You can observe the difference in an online Vue template explorer:

  1. condense
  2. preserve

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sign in alert for main page module connections

I need to set up a login-pop-up window to appear when a user clicks on links within a custom module. Currently, I am using rokbox to display the pop-up for other links, but I want it specifically for certain links within this module. If the user is not log ...

When Ajax sends an HTTP Get request to an MVC Controller with a complex JSON object as a parameter, the controller receives it as null

I am currently working with three classes: public class MainSearch { public MainSearch() { SearchData searchData = new SearchData(); SearchMode searchMode = new SearchMode(); } public SearchData searchData { get; set; } ...

How can I show express-validator errors in Angular application?

Struggling to integrate the express-validator with angularjs and html in order to show validation errors, but encountering difficulties accessing the errors variable. Check out the code snippet below. routes.js router.post('/register', functio ...

Trouble with sending data from jQuery Ajax to Node.js server

I am in the process of developing a video streaming platform that needs to constantly update the backend on the status of the videos being played. However, I am encountering an issue where the data sent through an ajax request appears as an empty object {} ...

Preventing Redundancy in Angular 2: Tips for Avoiding Duplicate Methods

Is there a way I can streamline my if/else statement to avoid code repetition in my header component? Take a look at the example below: export class HeaderMainComponent { logoAlt = 'We Craft beautiful websites'; // Logo alt and title texts @Vie ...

Tips on transitioning a Node.js application from JavaScript to TypeScript incrementally

I have a JavaScript node application that has grown quite large and I am considering migrating to TypeScript for faster development and easier code maintenance. I have installed TypeScript along with the node and mocha types using the following commands: ...

Accessing a specific attribute of an object contained within an array

Currently, I am utilizing Vue.js in my project and have implemented a method that involves comparing values from two different arrays. array1: [{ name: 'test1', somevar: true }, { name: 'test2', somevar: false }] array2: ['test1 ...

Using experimental.externalDir in NextJS prevents the use of absolute imports in external libraries

In my monorepo setup with nextjs, lerna, and npm workspaces, the folder structure is as follows: packages next-js-app pages index.tsx tsconfig.json ui-library src components dropdown. ...

Seeking advice on removing the initial blank space from a dropdown value in Angular.js

I have a deeply thought-out logic that I would like to illustrate with an example. However, in order to present it effectively, I am looking for suggestions on how to simplify the process without relying too heavily on the controller. <select class="fo ...

Refreshing ng-repeat dynamically with $http without reloading the page

Just a quick heads-up, I'm new to AngularJS In my AngularJS application, I am using the $http service to fetch data. Each row has an update button that needs to trigger a server-side method (the API is already returning data) to sync with a third-par ...

Clicking on an anchor tag will open a div and change the background color

.nav_bar { background: #c30015; margin-left: 50px; float: left; } .nav_bar ul { padding: 0; margin: 0; display: flex; border-bottom: thin white solid; } .nav_bar ul li { list-style: none; } .nav_bar ul li a { ...

Deliver search findings that are determined by matching criteria, rather than by identification numbers

I am seeking to return a JSON match upon form submission, rather than simply searching for a specific ID. However, I am uncertain about how to structure this. I have the ability to search for the necessary match in a JavaScript document within one of my n ...

Avoid the ability for individuals to interact with the icon embedded within a button

I'm currently working on a website where users need to interact with a button to delete an "Infraction." The button has a basic bootstrap style and includes an icon for the delete action. <button referencedInfraction="<%= i.infractionID %>" ...

Error: The function jQuery(...).validate does not exist

I am encountering an issue when trying to validate a dynamic html content in an edit form view. The error message "TypeError: jQuery(...).validate is not a function" keeps popping up despite using jQuery version 1.6.2. function validate_edit_venue() { ...

React | Rendering multiple elements

Is there a way to include multiple <div id = "x" /> <div id = "y" /> elements in my index.html file using REACT? My site is already built with all the templates in index.html, so I only need to use REACT for specific sections. https://i.sstati ...

Navigating through the array of words and deconstructing sentence formation

I'm having trouble running the parse button to display the results from the input box... Here is the code I am using: <html> <head> <title> sentence detector</title> </head> <body background="english.jpg"> <fon ...

Having trouble with your computed includes not working in Vue.js? Unsure of how to fix it?

How come when I use "includes" in Vue with my v-model, Vue.js logs an error? However, if I write (.includes("blabla)), everything works fine. What could be the issue? Also, how can I return the entire array if the (if) condition will work? For example, ...

What is the best way to link my "dynamic sub-component" with AngularJS?

While I have a solid grasp on the fundamentals of AngularJS and can create basic CRUD applications, when it comes to applying this knowledge to real-world scenarios, I find myself struggling with how to integrate the concepts effectively. One specific cha ...

Creating unique jQuery objects by comparing specific object properties

I am looking to create an array of unique objects by removing duplicate objects with specific property values. For example, consider the following code snippet where event1 and event2 have identical titles and start values, while event3 and event4 share th ...

Creating an expand and collapse animation in a `flex` accordion can be achieved even when the container size is fixed using

Good day, I am in need of a fixed-height HTML/CSS/JS accordion. The requirement is for the accordion container's height to be fixed (100% body height) and for any panel content overflow, the scrollbar should appear inside the panel's content div ...