Which is the better option for selecting DOM elements in a Vuejs 3 application: using raw js or jquery?

 I am currently working on developing an application using Node.js and Vue.js 3. One of the features I have implemented is a sidebar that dynamically fetches links from a routes file and displays them. The sidebar consists of a component that organizes a list of recursive link components.

 As the number of links grows and becomes recursive, I am facing challenges in handling class toggling (such as active, showing, collapsed) for each link and ensuring proper relationships between them (ensuring only one is active at a time) purely with Vue.js. Should I rely on querySelector or consider utilizing frameworks like jQuery to assist with this, or should I adhere to a purely Vue.js approach?

Edit:

 I am not seeking community opinions on this matter. My main goal is to gain a pragmatic understanding of why manipulating the DOM "outside" of Vue may or may not be advisable.

Answer №1

When working with Vue, it's best to let the framework be in control of the DOM rather than making direct changes yourself. This applies not only to Vue but also to other SPA frameworks like React and Angular.

The reason for this is that Vue operates by modifying the DOM on its own and expects to have full control over it. When rendering components, Vue removes old DOM elements, adds new ones, updates event bindings, and optimizes to only update necessary DOM nodes.

If you interfere and make direct changes that Vue is unaware of, your changes may get overwritten by Vue during the next render or conflict with bindings that Vue relies on.

While it's possible to work with Vue and jQuery together if you're knowledgeable about Vue's lifecycle, it's generally not recommended. Vue and jQuery function differently; jQuery modifies the DOM directly while Vue builds components that manage their own state and rendering.

Trying to use both frameworks together diminishes their individual advantages and adds complexity in managing conflicting state and rendering theories. It often proves easier to rewrite a jQuery widget in Vue directly rather than embedding it within a Vue app.

This shift requires changing habits developed from working with jQuery, such as building the entire DOM structure and then adding control structures afterward. In Vue, it's better to integrate logic into components so that the framework can handle the work for you.

I recommend creating one Vue component for each link that manages its own state for open/closed/active states and recurses to its children when "open". By approaching navigation data in this way, you allow Vue to manage the tree more efficiently compared to manually controlling it as you would with jQuery.

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

Error encountered with AngularJS code when attempting to load content from another page using ajax

I'm currently tackling a challenge with AngularJs and php. Whenever I try to load content from another page, AngularJs seems to stop working. Let me provide you with a sample code snippet to illustrate my issue. main-page.php <div id="form-secti ...

Unable to retrieve the /socket.io/socket.io.js file in the client-side application

My server side application is currently hosted on heroku: The code snippet that is relevant to this issue is as follows: const express = require('express'), app = express(), server = require('http').createServer(app), ...

What is the process of creating Vue.js single file components and incorporating them into a non-single page application?

Currently, I am in the process of developing a web application (specifically ASP.NET Core 2 MVC) that is not a single page application. Vue.js is being used to enhance the front-end functionality through a Vue instance. As the project progresses, there is ...

The ASP.NET MVC controller did not receive the JSON object properly due to some missing properties

I am facing an issue when trying to pass a JSON object to my ASP.NET MVC controller. The JSON is set in JavaScript in this way: jsonChildren = '{ "childImproItems" : [{ "Theme":"tralalali" }, { "Theme":"tralalali" }]}'; ... $.ajax({ ...

Obtain or save the created QR code using VueJs and the vue-qrcode plugin

I am utilizing the "vue-qrcode" plugin to create a QR code for my users linking to their public profile, which they can then easily share on platforms such as business cards. My goal is to provide users with the option to download the QR code with one but ...

How to Use Jquery to Perform Subtraction on Elements

Hello everyone! I am currently working on creating a nutrition label similar to this one: Example I have set up my database and now I am focusing on getting it to work with one element before moving on to the rest. Here is what my database looks like: in ...

Utilizing Jquery to add a delay and animate the sliding up effect on a recently created element

I am looking to apply a slide-up effect to a newly created element after a specified delay. $("div[data-error='true']").delay(5000).slideUp(500, function () { $("#error-alert").remove(); }); $("div[data-success='true']").delay(5000 ...

Why does the Angular page not load on the first visit, but loads successfully on subsequent visits and opens without any issues?

I am currently in the process of converting a template to Angular that utilizes HTML, CSS, Bootstrap, JavaScript, and other similar technologies. Within the template, there is a loader function with a GIF animation embedded within it. Interestingly, upon ...

Nested JSON file retrieval by Axios

I am having trouble retrieving the 'title' from my JSON file. I have been trying to access it through the 'results - metaData' path, but so far without success. The 'resultPacket' is returning data, but accessing 'metaDat ...

JavaScript node_modules import issue

I've been grappling with this issue for quite some time now. The problem lies in the malfunctioning of imports, and I cannot seem to pinpoint the exact reason behind it. Let me provide you with a few instances: In my index.html file, which is complet ...

What is the best way to create a deep clone of an XMLDocument Object using Javascript?

I am currently working on a project that involves parsing an XML file into an XMLDocument object using the browser's implementation of an XML parser, like this: new DOMParser().parseFromString(text,"text/xml"); However, I have encountered a situatio ...

Tips for efficiently transmitting JSON information from Nuxt Axios to a FastAPI server via a POST operation

Currently, I am attempting to transmit user data from Nuxt.js using Axios through a POST request. The data is being fetched via a JavaScript CDN function that returns an object containing user parameters, so avoiding the usage of a form is preferred as I w ...

Referencing a JSON object

Here is a JSON list of search terms: [ "halo", [ "halo reach", "halo anniversary", "halo 4", "halo 3", "halo mega bloks", "halo 2", "halo sleepsack", "halo wars", "halo reach xbox 360", "halo combat evolved" ], ...

ReactJs: Struggling to programmatically set focus on an element with React.createRef()

Looking to detect when a user clicks on an arrow using the keyboard? I stumbled upon the Arrow Keys React react module. To make it work, you need to call the focus method on a specific element. Manually clicking on an element does the trick: https://i.s ...

Using Angular JS, the JSON method is invoked from a location external to the controller

How can I call an inner method from outside a controller in AngularJS? var app; (function (){ app = angular.module("gallery", []); app.controller("galleryController", ['$scope','$http', function($scope, $http){ var gallery = this; ...

Dropdown in Angular JS is displaying incorrect values in the options

I am a newcomer to AngularJS and encountering an issue with populating a dropdown. Within a foreach loop of data retrieved from the server, I have created an array of user data: $scope.Users.push( { userid: ...

Configuring Axios header in Java backend based on the value stored in the express session

I am currently working on a frontend app using node.js express for server-side rendering. This app calls java backend endpoints and I use Axios to make the requests. A specific header named "agent-id" needs to be set in every request that is sent from expr ...

Unable to access socket.io due to a 404 error

I've encountered an issue while setting up a node server to serve a web page using socket.io. Upon loading the page, I noticed the following error in the console: (index):6 GET http://localhost:3000/socket.io/socket.io.js which is then followed by ...

Send form information using AJAX response

I have successfully implemented a feature where I load an iframe into a div with the id of #output using AJAX. This allows me to play videos on my website. jQuery( document ).ready( function( $ ) { $( '.play_next' ).on('click', fun ...

jQuery does not have the ability to manipulate a partially rendered content that was fetched via AJAX in Rails

One feature of my application is the ability to create "memos" and reply to other memos using @ tags within your own memo. For example: Hello, this is in response to @123 When you click on @123, memo #123 will be displayed beneath the memo you are curr ...